Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't log into PostgreSQL database

Tags:

I created a user like this:

create user blog with password 'blog'; 

Then I made it the owner of a database:

alter database blog_development owner to blog; 

Then I tried to log in and it didn't work:

$ psql -d blog_development -U blog -W Password for user blog: psql: FATAL:  Ident authentication failed for user "blog" 

Any idea why?

One thing I've tried is editing pg_hba.conf

 76 # Database administrative login by UNIX sockets  77 local   all                postgres                          ident  78  79 # TYPE  DATABASE           USER        CIDR-ADDRESS          METHOD  80  81 # "local" is for Unix domain socket connections only  82 local   all                all                               ident  83 # IPv4 local connections:  84 host    all                all         127.0.0.1/32          md5  85 host    blog_development   blog        127.0.0.1/32          md5  86 # IPv6 local connections:  87 host    all                all         ::1/128               md5 

Line 85 is the one I added. I restarted PostgreSQL after that but it didn't seem to change anything.

like image 453
Jason Swett Avatar asked Dec 30 '10 19:12

Jason Swett


People also ask

Can't connect to PostgreSQL database?

First, double check that the Postgres process is running where you expect it to be. If you are trying to connect to a Postgres instance on the same host as your terminal, you can run lsof -p :5432 which will show which, if any, processes are listening on that port. The postgres process should be connected there.

How do I fix postgres password authentication failed?

Restart the PostgreSQL service from the Services control panel ( start->run->services. msc ) Connect using psql or pgAdmin4 or whatever you prefer. Run ALTER USER postgres PASSWORD 'fooBarEatsBarFoodBareFoot'

Why is postgres connection refused?

Firewall restrictions At times, the restrictions placed by the server firewall can also trigger connection refused error. If the psql port 5432 is not open for connection or if there is any restriction on the IP address from which the TCP/IP connection occurs, it may trigger connection refused error.


2 Answers

I was just trying to connect in the wrong way. Here's the right way:

psql -h localhost -U blog -d blog_development 
like image 53
Jason Swett Avatar answered Nov 15 '22 12:11

Jason Swett


psql uses a local socket by default (check out -h flag in man) - that should match line 82 when you log in.

82 local all all ident

ident needs a real user in the system to check the password against. So you can either connect by host or add an user so it matches you new config line.

like image 25
nate c Avatar answered Nov 15 '22 13:11

nate c