Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow postgres login without password prompt on local connection?

Tags:

postgresql

How do you configure PostgreSQL to allow the default postgres user to login without a password from localhost?

There are several similar questions, such as this and this, but none of the suggestions to modify pg_hba.conf have worked for me. However, based on my understanding of the pg_hba.conf rules, I should be able to accomplish this with either the peer or trust options.

I'm trying to run a command like:

sudo psql --user=postgres --no-password --command="blah;" -h 127.0.0.1

If I try this line in my pg_hba.conf:

local   all             postgres                                trust

my command fails with the error:

psql: FATAL:  no pg_hba.conf entry for host "127.0.0.1", user "postgres", database "postgres", SSL on
FATAL:  no pg_hba.conf entry for host "127.0.0.1", user "postgres", database "postgres", SSL off

If I tried this line:

local   all             postgres                                peer

my command fails with the same error.

How do I fix this?

like image 496
Cerin Avatar asked Oct 13 '17 19:10

Cerin


People also ask

How do I connect to PostgreSQL without a password?

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. The "C:\Program Files\PostgreSQL\12\data" is the data directory. PostgreSQL will not require a password to login.


1 Answers

When you specify -h 127.0.0.1 that is not a "local" connection - it's a host (=TCP based) connection. So a line starting with local will not match for such a connection.

To make that entry work, do not specify a hostname or port for psql, then it will use a "local" connection through named pipes.

Alternatively, you can local with host and then add 127.0.0.1 as the (client) IP address to "trust".

like image 143
a_horse_with_no_name Avatar answered Oct 04 '22 10:10

a_horse_with_no_name