Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect as user with no password set on Postgresql 8.4 via JDBC

I'm trying to connect to a PostgreSQL 8.4 DB in Ubuntu 10.10 via the JDBC drivers. I'm connecting via jdbc:postgresql:localhost:5433/dbname because PostgreSQL is running on a non-default port 5433 so I have to specify the port.

I've already edited my postgresql.conf to set listen_addresses = "*". I understand that even though it's localhost, it's still using TCP/IP to connect via JDBC.

My problem is that I created a user without a password. If i do not specify a password with DriverManager.connect(url), it errors indicating that I need to specify a password for authentication. Every password I try, including empty string, fails to authenticate with the DB.

How can I connect?

Edit: If connecting over wrong port, the error is : PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections. When attempting to connect on the correct port, I'm getting PSQLException: FATAL: password authentication failed for user "user". THis is remedied by the accepted answer below.

like image 206
Stealth Rabbi Avatar asked Nov 13 '12 22:11

Stealth Rabbi


People also ask

How do I log into Postgres without password?

Edit the pg_dba. conf file and change all local connections from md5 to trust. By doing this, you can log in to the PostgreSQL database server without using a password.

How do I login as Postgres user?

There are two ways to login PostgreSQL: By running the "psql" command as a UNIX user which is also configured as PostgreSQL user using so-called IDENT/PEER authentication, e.g., " sudo -u postgres psql ". Via TCP/IP connection using PostgreSQL's own managed username/password (using so-called MD5 authentication).

Should Postgres user have a password?

For most systems, the default Postgres user is postgres and a password is not required for authentication.


1 Answers

If you have pg_hba.conf set to require md5 authentication and the user has no password, then no authentication can occur.

ALTER USER the_user_name PASSWORD 'give_it_a_password';

Alternately, use ident or (for localhost only, unsafe) trust authentication for that user/db combo in pg_hba.conf if you really must have no password. This is usually a bad idea, it's much better to just set a password.

Demo:

$ psql -q -U postgres postgres
postgres=# CREATE USER nopw;
CREATE ROLE

$ psql -h localhost -U nopw postgres
Password for user nopw:               [pressed enter]
psql: fe_sendauth: no password supplied

$ psql -q -U postgres postgres
postgres=# ALTER USER nopw PASSWORD 'test';
postgres=# \q

$ psql -q -h localhost -U nopw postgres
Password for user nopw:            [entered 'test' then pressed enter]
postgres=> 
like image 161
Craig Ringer Avatar answered Sep 28 '22 07:09

Craig Ringer