226

when i create a new user, but it cannot login the database.
I do that like this:

postgres@Aspire:/home/XXX$ createuser dev
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y

then create a database:

postgres@Aspire:/home/XXX$ createdb -O dev test_development

after that, I try psql -U dev -W test_development to login, but get the error:

psql: FATAL:  Peer authentication failed for user "dev"

I tried to solve the problem but failed.

2

12 Answers 12

367

Try:

psql -U user_name  -h 127.0.0.1 -d db_name

where

  • -U is the database user name
  • -h is the hostname/IP of the local server, thus avoiding Unix domain sockets
  • -d is the database name to connect to

This is then evaluated as a "network" connection by Postgresql rather than a Unix domain socket connection, thus not evaluated as a "local" connect as you might see in pg_hba.conf:

local   all             all                                     peer
6
  • 15
    I needed (ver 9.4): psql -U user-name -h 127.0.0.1 -d db-name
    – Gregor
    Dec 12, 2014 at 0:05
  • 9
    what an ornery tool. the manual says psql [option...] [dbname [username]], so you'd think psql dbname username would just work..
    – djeikyb
    Aug 6, 2015 at 21:04
  • 2
    Worked for me as well. I also think this is a much more preferable way than changing config files, especially when you don't have a clue about what you are doing and just following SO answer to solve your problem.
    – borisano
    Nov 24, 2015 at 15:07
  • 2
    Good answer. I was getting from inside the database after having logged in as postgres \c glossary john FATAL: Peer authentication failed for user "john" then with \c glossary john localhost Password for user john: SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off) You are now connected to database "glossary" as user "john" on host "localhost" at port "5432". it worked.
    – look
    Dec 4, 2016 at 2:26
  • 2
    Why is the -h required? I have Postgres databases that require it and ones that don't. What's the difference?
    – d512
    Apr 13, 2017 at 20:17
237

Your connection failed because by default psql connects over UNIX sockets using peer authentication, that requires the current UNIX user to have the same user name as psql. So you will have to create the UNIX user dev and then login as dev or use sudo -u dev psql test_development for accessing the database (and psql should not ask for a password).

If you cannot or do not want to create the UNIX user, like if you just want to connect to your database for ad hoc queries, forcing a socket connection using psql --host=localhost --dbname=test_development --username=dev (as pointed out by @meyerson answer) will solve your immediate problem.

But if you intend to force password authentication over Unix sockets instead of the peer method, try changing the following pg_hba.conf* line:

from

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          peer

to

# TYPE DATABASE USER ADDRESS METHOD
local  all      all          md5
  • peer means it will trust the identity (authenticity) of UNIX user. So not asking for a password.

  • md5 means it will always ask for a password, and validate it after hashing with MD5.

You can, of course, also create more specific rules for a specific database or user, with some users having peer and others requiring passwords.

After changing pg_hba.conf if PostgreSQL is running you'll need to make it re-read the configuration by reloading (pg_ctl reload) or restarting (sudo service postgresql restart).

* The file pg_hba.conf will most likely be at /etc/postgresql/9.x/main/pg_hba.conf

Edited: Remarks from @Chloe, @JavierEH, @Jonas Eicher, @fccoelho, @Joanis, @Uphill_What comments incorporated into answer.

5
  • 5
    How do you allow peer AND md5? When I set md5, then I can no longer login with postgres user! I tried adding multiple lines, and separating the method with commas, but they didn't work. Ok I found mgoldwasser's answer and that worked. I just added another line for user postgres with method peer!
    – Chloe
    Jul 6, 2014 at 4:44
  • 3
    Dont worry, you can set peer authentication for specific users (for example, your username or postgres). It seems that specific rules override general rules
    – JavierIEH
    Nov 23, 2014 at 3:29
  • 1
    On some distributions the file might also be found here: /var/lib/pgsql/9.4/data/pg_hba.conf Apr 7, 2015 at 17:01
  • 2
    can't you do this without restarting postgresql? can't you just specify the authentication method in the pg_user table?
    – fccoelho
    May 9, 2015 at 12:49
  • 2
    You can reload instead of restart. That reloads the pg_hba table. Worked for me.
    – Joanis
    Jun 1, 2015 at 14:08
30

Peer authentication means that postgres asks the operating system for your login name and uses this for authentication. To login as user "dev" using peer authentication on postgres, you must also be the user "dev" on the operating system.

You can find details to the authentication methods in the Postgresql documentation.

Hint: If no authentication method works anymore, disconnect the server from the network and use method "trust" for "localhost" (and double check that your server is not reachable through the network while method "trust" is enabled).

2
  • 1
    thanks for your answers.but it still don't work if i change the dev to system user XXX
    – hsming
    Jul 3, 2013 at 9:29
  • 2
    stefan's answer is correct. I just added a link, which will be visible when my edit is reviewed, to the documentation where each of the authentication methods are explained.
    – dsh
    Jul 3, 2013 at 11:46
25

When you specify:

psql -U user

it connects via UNIX Socket, which by default uses peer authentication, unless specified in pg_hba.conf otherwise.

You can specify:

host    database             user             127.0.0.1/32       md5
host    database             user             ::1/128            md5

to get TCP/IP connection on loopback interface (both IPv4 and IPv6) for specified database and user.

After changes you have to restart postgres or reload it's configuration. Restart that should work in modern RHEL/Debian based distros:

service postgresql restart

Reload should work in following way:

pg_ctl reload

but the command may differ depending of PATH configuration - you may have to specify absolute path, which may be different, depending on way the postgres was installed.

Then you can use:

psql -h localhost -U user -d database

to login with that user to specified database over TCP/IP. md5 stands for encrypted password, while you can also specify password for plain text passwords during authorisation. These 2 options shouldn't be of a great matter as long as database server is only locally accessible, with no network access.

Important note: Definition order in pg_hba.conf matters - rules are read from top to bottom, like iptables, so you probably want to add proposed rules above the rule:

host    all             all             127.0.0.1/32            ident
1
  • re the Important note: - order is important +1
    – hawkeye
    Jan 4, 2020 at 10:51
24

While @flaviodesousa's answer would work, it also makes it mandatory for all users (everyone else) to enter a password.

Sometime it makes sense to keep peer authentication for everyone else, but make an exception for a service user. In that case you would want to add a line to the pg_hba.conf that looks like:

local   all             some_batch_user                         md5

I would recommend that you add this line right below the commented header line:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
local   all             some_batch_user                         md5

You will need to restart PostgreSQL using

sudo service postgresql restart

If you're using 9.3, your pg_hba.conf would most likely be:

/etc/postgresql/9.3/main/pg_hba.conf

13

This works for me when I run into it:

sudo -u username psql
1
  • only works if you create a whole new user on the system AND in postgres -- hsming just wants to connect to postgres with the newly created postgres user, dev.
    – Kenneth
    Nov 30, 2015 at 1:04
9

I simply had to add -h localhost

1
  • My issue was using postgres on a Raspberry. ^^^ worked for me!! Thank you!
    – tidydee
    Jul 25, 2019 at 18:16
8

The easiest solution:

CREATE USER dev WITH PASSWORD 'dev';
CREATE DATABASE test_development;
GRANT ALL PRIVILEGES ON DATABASE test_development to dev;
ALTER ROLE dev CREATEROLE CREATEDB;
2

In my case I was using different port. Default is 5432. I was using 5433. This worked for me:

$ psql -f update_table.sql -d db_name -U db_user_name -h 127.0.0.1 -p 5433
1
  • This can occure automatically when you have more than one installation of postgres.
    – Galigator
    Oct 30, 2017 at 14:18
1

For people in the future seeing this, postgres is in the /usr/lib/postgresql/10/bin on my Ubuntu server.

I added it to the PATH in my .bashrc file, and add this line at the end

PATH=$PATH:/usr/lib/postgresql/10/bin

then on the command line

$> source ./.bashrc

I refreshed my bash environment. Now I can use postgres -D /wherever from any directory

1

pg_dump -h localhost -U postgres -F c -b -v -f mydb.backup mydb

1
  • What question does this answer? Why might -U postgres be pivotal? What are all those dash-letters, anyway?
    – greybeard
    Apr 19, 2020 at 6:45
0

Try in terminal:

>> psql -U role_name -d database -h hostname.<domain>.com -W
1
  • Please add a little more context to you answer.
    – Thoth
    Feb 7, 2020 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.