1153

I'm getting the error:

FATAL: Peer authentication failed for user "postgres"

when I try to make postgres work with Rails.

Here's my pg_hba.conf, my database.yml, and a dump of the full trace.

I changed authentication to md5 in pg_hba and tried different things, but none seem to work.

I also tried creating a new user and database as per Rails 3.2, FATAL: Peer authentication failed for user (PG::Error)

But they don't show up on pgadmin or even when I run sudo -u postgres psql -l.

Any idea where I'm going wrong?

Joel Ellis
  • 1,332
  • 1
  • 12
  • 31
orderof1
  • 11,831
  • 4
  • 18
  • 18
  • 1
    **1):** Make sure you have a user named "postgres" created and having right on your Database **2):** Make sure he has a password **3):** Make sure your config/database.yml contains the rights credentials (username + password) – MrYoshiji Sep 06 '13 at 18:19
  • 2
    need everyplace peer and indent set to md5 – Artem.Borysov Jul 28 '15 at 08:55
  • 36
    See **[also this answer](http://stackoverflow.com/a/29803697/441652)** - connection may fail on `localhost` but succeed on `127.0.0.1`. – uvsmtid Aug 26 '15 at 07:13
  • 14
    In my case I needed to add `host: localhost` in the `database.yml` file. – Mihail Velikov Mar 16 '16 at 06:50
  • this link helped me http://suite.opengeo.org/docs/latest/dataadmin/pgGettingStarted/firstconnect.html – Sudip Bhandari Apr 25 '17 at 10:35
  • I fixed this error by appending `--host=localhost` which I assumed to be the default – Collin Sep 05 '19 at 22:20
  • Refer this for `postgres` authentication error [Link](https://stackoverflow.com/questions/55038942/fatal-password-authentication-failed-for-user-postgres-postgresql-11-with-pg/65750770#65750770) – Gaurav Patil Aug 17 '21 at 04:28

28 Answers28

1476

The problem is still your pg_hba.conf file*.

This line:

local   all             postgres                                peer

Should be:

local   all             postgres                                md5

After altering this file, don't forget to restart your PostgreSQL server. If you're on Linux, that would be sudo systemctl restart postgresql (on older systems: sudo service postgresql restart).


Locating hba.conf

Note that the location of this file isn't very consistent.

You can use locate pg_hba.conf or ask PostgreSQL SHOW hba_file; to discover the file location.

Usual locations are /etc/postgresql/[version]/main/pg_hba.conf and /var/lib/pgsql/data/pg_hba.conf.


These are brief descriptions of the peer vs md5 options according to the official PostgreSQL docs on authentication methods.

Peer authentication

The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.

Password authentication

The password-based authentication methods are md5 and password. These methods operate similarly except for the way that the password is sent across the connection, namely MD5-hashed and clear-text respectively.

If you are at all concerned about password "sniffing" attacks then md5 is preferred. Plain password should always be avoided if possible. However, md5 cannot be used with the db_user_namespace feature. If the connection is protected by SSL encryption then password can be used safely (though SSL certificate authentication might be a better choice if one is depending on using SSL).

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
  • 76
    You'll need to reload your postgresql service after changing this `/etc/init.d/postgresql reload` – funkotron Oct 28 '13 at 13:50
  • 88
    putting this here since I always forget where this file is `/etc/postgresql/9.1/main/pg_hba.conf` – Doug Jan 30 '14 at 21:08
  • 17
    @funkotron At least on my ElementaryOS (Ubuntu) installation, `sudo service postgreql restart` also works. – Marnen Laibow-Koser Mar 21 '14 at 17:47
  • 1
    What exactly does this change do? I don't want to blindly make changes in hopes of it working. – Dennis Mar 28 '14 at 15:05
  • 19
    To answer my own question: 'peer' authentication means that postgres asks the operating system for your login name and uses this for authentication, so the user on the OS and on postgres must be the same. 'md5' uses encrypted password authentication. – Dennis Mar 28 '14 at 15:17
  • 13
    I understand the change. But why isn't this the default behaviour? Is there any drawback using md5? – Victor Ribeiro Jun 13 '15 at 19:31
  • 3
    Make sure your granting rule is put *before* any other matching but denying rule in `pg_hba.conf`. [As PostgreSQL 9.3's documentation on `pg_hba.conf`](http://www.postgresql.org/docs/9.3/static/auth-pg-hba-conf.html) states: "*The first record with a matching connection type, client address, requested database, and user name is used to perform authentication. There is no "fall-through" or "backup": if one record is chosen and the authentication fails, subsequent records are not considered.*" – Abdull May 03 '16 at 16:45
  • 5
    on CentOS 6.5 I found this file in `/var/lib/pgsql/9.1/data` – botheredbybees Dec 02 '16 at 02:29
  • 1
    Hi, is not working at UBUNTU 16 LTS with pg9.5... Do you have a **complete `pg_hba.conf` and `postgresql.conf` scripts**, adapted to this env and permissions? I need to use (in ssh terminal at server) `PGPASSWORD=postgres psql -U postgres`. – Peter Krauss Jun 20 '17 at 11:04
  • 4
    response below by Arivarasan with Artem edits is actually better. this solution above did not work, at least for me (Elementary OS). See this [link](https://www.postgresql.org/message-id/b9e2597c0905061106o1db54c83gae31f44bf0d27902@mail.gmail.com) to Postgres support answer that basically says the same. you need to replace 'peer' with 'trust' first and then change it to 'md5' – ng10 Feb 25 '18 at 20:54
  • 4
    Uhh it is asking for a password after changing to md5... what is the password? – Valachio May 02 '18 at 17:35
  • 1
    >> Valachio: I set the password for user postgres so: 1. su, 2. su - postgres, 3. psql (or psql -p ), 4. \password, 5. \q – mirek May 03 '18 at 12:06
  • @ng10 is correct. We need to deal with the default `postgres` user first by setting method to `trust` as https://stackoverflow.com/a/26735105/248616 – Nam G VU May 04 '18 at 08:47
  • I don't even have postgresql under /etc – WTIFS May 11 '18 at 13:10
  • on CentOS 7, Amazon Linux, this config file is in here: `/var/lib/pgsql/9.6/data/pg_hba.conf` – kmonsoor Jun 14 '18 at 00:37
  • Updating for any version:, to check the conf file: `sudo -u postgres psql -c "SHOW config_file"` – Peter Krauss Jul 16 '18 at 02:30
  • 2
    Works on Fedora, `pg_hba.conf` lies under `/var/lib/pgsql/data` – Qortex May 17 '19 at 18:46
  • 1
    Please tell me the "md5" setting doesn't actually use md5, which is completely broken for passwords. – Joel Coehoorn Sep 25 '19 at 19:35
  • make sure to use this link as well https://ubuntu.com/server/docs/databases-postgresql – Shomaail May 06 '20 at 10:08
  • sed script to automatically replace this: `sudo sed -i 's/local all postgres peer/local all postgres md5/' pg_hba.conf` – Doug Jul 07 '20 at 16:22
  • I have postgresql V12 on Centos 7. `pg_hba.conf` is exists on '/var/lib/pgsql/12/data/pg_hba.conf'. for restarting the postgres should use `sudo systemctl restart postgresql-12` althou – Mohammad Reza Oct 19 '20 at 07:44
  • After applying this solution, restart postgres by following command: `sudo service postgresql restart` – Farid Chowdhury Apr 07 '21 at 11:11
  • Changing to md5 is only one step to take - such settings make clear that a pw is the authentification method. Clearly, if you do not yet have a password set for your super user, you still need to give that super user a password. An `ALTER` command cannot do that in such a situation if your `pg_hba.conf` is not set to `trust`. Therefore and instead, you need (credits to @mirek's comment): `sudo su postgres`, `psql *(or psql -p if you have more than one PostgreSQL)*`, `\password`, `\q` – questionto42 Sep 14 '21 at 20:53
  • 1
    To check location of pg_hba.conf connect to postgres db using psql then type `SHOW hba_file;` command. – Vivek Oct 05 '21 at 09:53
659

After installing Postgresql I did the below steps.

  1. Open the file pg_hba.conf. For Ubuntu, use for example /etc/postgresql/13/main$ sudo nano pg_hba.conf and change this line at the bottom of the file, it should be the first line of the settings:

    local   all             postgres                                peer

    to

    local   all             postgres                                trust

    Side note: If you want to be able to connect with other users as well, you also need to change:

    local   all             all                                peer

    to

    local   all             all                                md5

    If you used nano editor, exit with double Escape, x, y, Enter to save the config file.

  2. Restart the server

     $ sudo service postgresql restart
    

    Output: * Restarting PostgreSQL 13 database server

  3. Login into psql and set your password

     $ psql -U postgres
     db> ALTER USER postgres with password 'your-pass';
    

    Output: ALTER ROLE

    Side note: If you have other users, they will need a password as well:

     db> ALTER USER my_user with password 'your-pass';
    

    Then enter:

     exit
    
  4. Finally change the pg_hba.conf from

    local   all             postgres                                trust

    to

    local   all             postgres                                md5
  5. Restart the server again

     $ sudo service postgresql restart
    

    Output: * Restarting PostgreSQL 13 database server

  6. Login at psql with postgres user

    After restarting the postgresql server, the postgres user accepts the password that you chose:

     psql -U postgres
    

    Output:
    Password for user postgres:

    psql (13.4 (Ubuntu 13.4-1.pgdg20.04+1))

    Type "help" for help.

    And you are in psql:

    postgres=#

    Side note: Same now works for my_user if you added the user and password:

     psql -d YOUR_DB_NAME -U my_user
    

    Which will ask you for the new password of my_user.

Authentication methods details:

trust - anyone who can connect to the server is authorized to access the database

peer - use client's operating system user name as database user name to access it.

md5 - password-base authentication

for further reference check here

questionto42
  • 7,175
  • 4
  • 57
  • 90
Arivarasan L
  • 9,538
  • 2
  • 37
  • 47
  • 34
    changing the method to "trust" worked for me. +1 for explanation of authentication method details. – La-comadreja Nov 18 '14 at 22:46
  • 8
    On OS X homebrew the default is trust, while on Ubuntu somehow the default is set to "peer", which led to discrepancies between my setup and that of my colleague. We changed his to MD5 which didn't help, so "trust" is the real answer here(We're only doing development testing). Should get more upvotes. – xji Nov 22 '14 at 14:42
  • 2
    you also can set md5 everyplace from the very beginning – Artem.Borysov Jul 28 '15 at 08:50
  • 2
    This way is working for me, using method md5 first was not. – sianipard Sep 16 '16 at 22:39
  • need to use same passwords? `sudo passwd postgres` – Peter Krauss Aug 27 '17 at 15:52
  • Hi, I followed the steps and changed my password. But after I changed trust to md5, close nano, reloaded the server with 'sudo service postgresql reload' And tried to login again using psql -U postgres It asked me for my password and I provided that but it said password authentication failed for user "postgres" I was on DigitalOcean one click ruby on rails. – Muhammad Nasir Shamshad Sep 19 '17 at 10:56
  • @ArivarasanL Why we need to set postgres method back to `md5` aka. why not just keep it `trust` for localhost? – Nam G VU May 04 '18 at 08:16
  • Updating for any version:, to check the conf file: `sudo -u postgres psql -c "SHOW config_file"` – Peter Krauss Jul 16 '18 at 02:30
  • I read this and I initially thought changing this to `trust` would allow anyone, anywhere to log in to the server, but after reading the docs on [local](https://www.postgresql.org/docs/13/auth-pg-hba-conf.html) I think I understand why this is okay for my use case. [Unix domain sockets](https://en.wikipedia.org/wiki/Unix_domain_socket) appear to be a form of IPC and only communicate between processes *on the same OS*, so this server should be inaccessible to any machine other than my dev PC. – jrh Feb 28 '21 at 18:34
  • Works perfectly, md5 would not work for me either. – Jeff Bluemel Aug 03 '21 at 23:29
  • @JeffBluemel Just changing to md5 (most voted answer!??) did not work for me either. md5 is only needed at the very end of it all *after* having set the pw of the postgres user. In the meantime, while setting up the passwords, you should connect as postgres with setting `trust`. Then restart, then login with postgres user with a pw, and from there, you can always change the other pw of other users. In the end, change all previously `peer` to `md5`. – questionto42 Sep 10 '21 at 12:13
  • Thanks a bunch, it finally worked. Why are the ipv4 and ipv6 sections used? Do we need it if we need to ssl remotely? – wallop Oct 03 '22 at 15:36
  • Thanks for this one, now I can run rails commands and they work – Boris Barroso Oct 25 '22 at 13:49
340

If you connect over localhost (127.0.0.1) you shouldn't experience that particular issue. I wouldn't muck much with the pg_hba.conf but instead I would adjust your connection string:

psql -U someuser -h 127.0.0.1 database

where someuser is your user you're connecting as and database is the database your user has permission to connect to.

Here is what I do on Debian to setup postgres:

http://www.postgresql.org/download/linux/debian/  (Wheezy 7.x)

as root …

    root@www0:~# echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list

    root@www0:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

    root@www0:~# apt-get update

    root@www0:~# apt-get install postgresql-9.4        

    root@www0:~# su - postgres 

    postgres@www0:~$ createuser --interactive -P someuser
    Enter password for new role:
    Enter it again:
    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) n

    postgres@www0:~$ createdb -O someuser database

    postgres@www0:~$ psql -U someuser -h 127.0.0.1 database

Enjoy!

StylusEater
  • 3,769
  • 1
  • 15
  • 13
  • 41
    I think this is the best solution as it actually solves the local problem without messing without messing with global configuration as the other answers recommend. – jeteon Jan 31 '16 at 04:47
  • 2
    I had to change `host: 127.0.0.1` from defaulting to localhost in config/database.yml - it's on the same machine so I don't understand why – Anthony Jan 26 '17 at 22:21
  • Anthony, are you saying you experienced a connection failure until you adjusted localhost to 127.0.0.1 in your database.yml? I suggest checking your /etc/hosts file if that's the case. Otherwise, there might be some other oddity related to resolving localhost. – StylusEater Jan 29 '17 at 16:41
  • 5
    This is much more elegant when compared to messing with the local peer authentication method. – mdh Feb 25 '17 at 14:18
  • 2
    This just hits me again, thinking of beeing on localhost ist not the same as telling this to psql. The real problem ist, that at least on *nix OSs psql otherwise will try to connect via local socket, which is by default only allowed for the user postgres. – normic Jan 20 '19 at 23:21
  • This solution worked for me on Ubuntu 20, with a slight change to uppercase -H as an option name – Lauris Nov 20 '20 at 18:43
  • This is a life saver. Tried so many solutions and failed when starting a embedded psql server in a repo. This worked like a charm – Kalana Dananjaya Dec 04 '22 at 10:22
  • On Ubuntu, can also create an alias for this method in `~/.bash_aliases`: `psql_local() { psql -h 127.0.0.1 $1 $2 $3 $4 } ` and then use like `psql_local -U user_name -d database` – Rimov Apr 12 '23 at 13:31
103

This has worked for me !!

sudo -u postgres psql
iamuser2
  • 1,041
  • 1
  • 7
  • 4
40
sudo psql --host=localhost --dbname=database-name --username=postgres

This solved my issue

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Gihan Gamage
  • 2,944
  • 19
  • 27
25

If you have an issue, you need to locate your pg_hba.conf. The command is:

find / -name 'pg_hba.conf' 2>/dev/null

and after that change the configuration file:

Postgresql 9.3

Postgresql 9.3

Postgresql 9.4

Postgresql 9.3

The next step is: Restarting your db instance:

service postgresql-9.3 restart

If you have any problems, you need to set password again:

ALTER USER db_user with password 'db_password';

d.danailov
  • 9,594
  • 4
  • 51
  • 36
  • 1
    thank you for the find command! using the fedoara file explorer never returned pg_hba.conf in any searches. now i can finally update that file – JesseBoyd Feb 06 '23 at 02:19
24

Simplest solution without changing configs. (ubuntu) Change user, then connect to database cli.

sudo -i -u postgres

psql

taken from https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04

Rokas Lakštauskas
  • 1,078
  • 1
  • 8
  • 17
23
  1. Go to this /etc/postgresql/9.x/main/ and open pg_hba.conf file

In my case:

$>  sudo nano /etc/postgresql/9.3/main/pg_hba.conf
  1. Replace peer with md5

So this will be changed to:

Database administrative login by Unix domain socket local all postgres peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

This:

Database administrative login by Unix domain socket local all postgres md5

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
  1. Then restart pg server:

    $> sudo service postgresql restart

Below is list of METHODS used to connect with postgres:

# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi",
# "krb5", "ident", "peer", "pam", "ldap", "radius" or "cert".  Note that
# "password" sends passwords in clear text; "md5" is preferred since
# it sends encrypted passwords.

Note: If you have not create you postgres user yet. Create that and now you can access postgres server using that user credentials.

TIP: If it does not work after postgres restart then close terminal and open again.

Taimoor Changaiz
  • 10,250
  • 4
  • 49
  • 53
15

I was moving data directory on a cloned server and having troubles to login as postgres. Resetting postgres password like this worked for me.

root# su postgres
postgres$ psql -U postgres
psql (9.3.6) 
Type "help" for help. 
postgres=#\password 
Enter new password: 
Enter it again: 
postgres=#
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
Luca Marletta
  • 457
  • 5
  • 13
15

This error may occur when you do not provide the host. The following scenario resembles it.

user@homepc:~$ psql -d test_db -U test_user
psql: error: FATAL: Peer authentication failed for user "test_user"
user@homepc:~$ psql -h localhost -d test_db -U test_user
Password for user test_user:

Providing host resolved my issue in psql command line. Try providing host in connection configuration for postgress in rails.

12

I had the same problem.

The solution from depa is absolutely correct.

Just make sure that u have a user configured to use PostgreSQL.

Check the file:

$ ls /etc/postgresql/9.1/main/pg_hba.conf -l

The permission of this file should be given to the user you have registered your psql with.

Further. If you are good till now..

Update as per @depa's instructions.

i.e.

$ sudo nano /etc/postgresql/9.1/main/pg_hba.conf

and then make changes.

nirvanastack
  • 455
  • 2
  • 5
  • 13
  • I am facing same issue. But What permissions I should need I don't know. can you help me. I am getting the output for the 'ls' command is `-rw-r----- 1 postgres postgres 4640 Jun 16 09:59 /etc/postgresql/9.5/main/pg_hba.conf` – Sun Jun 16 '18 at 07:48
12

If you want to keep the default config but want md5 authentication with socket connection for one specific user/db connection, add a "local" line BEFORE the "local all/all" line:

# TYPE  DATABASE     USER         ADDRESS             METHOD

# "local" is for Unix domain socket connections only
local   dbname       username                         md5  # <-- this line
local   all          all                              peer
# IPv4 local connections:
host    all          all          127.0.0.1/32        ident
# IPv6 local connections:
host    all          all          ::1/128             ident
Åsmund
  • 1,332
  • 1
  • 15
  • 26
  • The "BEFORE" in this answer deserves more elaboration in other answers. It turns out psql examines records in the pg_hba.conf file sequentially as stated in the docs: https://www.postgresql.org/docs/11/static/auth-pg-hba-conf.html – eaydin Nov 01 '18 at 16:56
  • # TYPE DATABASE USER ADDRESS METHOD local username dbname md5 # <-- this line Not in order – Kiry Meas Aug 08 '19 at 04:01
  • @KiryMeas Indeed. Fixed. – Åsmund Nov 16 '20 at 13:05
10

Most of the solutions are suggest editing the pg_hba.conf.

For you who don't want to edit the configuration file, you basically just need to log in to the postgres user. If you are using/in Linux server, use this command

sudo -i -u postgres

It'll create user postgres and then log in to it. Now try your psql command again.

You can also add postgres user a password with the command: (you should be in root user)

passwd postgres

This is works because according to this PostgreSQL's Documentation,

Peer Authentication

The peer authentication method works by obtaining the client's operating system user name from the kernel and using it as the allowed database user name (with optional user name mapping). This method is only supported on local connections.

Dharman
  • 30,962
  • 25
  • 85
  • 135
alramdein
  • 810
  • 2
  • 12
  • 26
7

The edits above worked for me, after I figured out that I needed to restart the postgres server after making them. For ubuntu:

sudo /etc/init.d/postgresql restart
sibosop
  • 91
  • 1
  • 3
6

Use host=localhost in connection.

PGconn *conn = PQconnectdb(
    "host=localhost user=postgres dbname=postgres password=123"
);
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
MageDev
  • 271
  • 1
  • 7
  • 27
6

Changing METHOD peer to trust in pg_hba.conf (/etc/postgresql/9.1/main/pg_hba.conf | line 85) solves the issue. Adding md5 asks for a password, hence if there is a requirement to avoid using passwords, use trust instead of md5.

6

Please follow the below steps

1). First, navigate to the /etc/postgresql/{your pg version}/main directory.

My version is 10 Then:

cd /etc/postgresql/10/main

2). Here resides the pg_hba.conf file needs to do some changes here you may need sudo access for this.

sudo nano pg_hba.conf

3). Scroll down the file till you find this –

# Database administrative login by Unix domain socket
local   all             postgres                                peer

4). Here change the peer to md5 as follows.

# Database administrative login by Unix domain socket
local   all             all                                md5
  • peer means it will trust the authenticity of UNIX user hence does not

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

5).Now save the file and restart the Postgres server.

sudo service postgresql restart

Now it should be ok.

Saranga kapilarathna
  • 564
  • 1
  • 12
  • 25
5

the below command works for me:

psql -d myDb -U username -W
Mazen Ora
  • 71
  • 1
  • 2
  • man psql `-W --password Force psql to prompt for a password before connecting to a database. This option is never essential, since psql will automatically prompt for a password if the server demands password authentication. However, psql will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing -W to avoid the extra connection attempt.` – Yaroslav Nikitenko Dec 07 '15 at 18:04
5

You need just set METHOD to trust.

#TYPE  DATABASE        USER            ADDRESS                 METHOD
local    all             all                                     trust

And reload postgres server.

# service postgresql-9.5 reload

Changes in pg_hba.conf dont require RESTART postgres server. just RELOAD.

5

pg_config is for compliation information, to help extensions and client programs compile and link against PostgreSQL. It knows nothing about the active PostgreSQL instance(s) on the machine, only the binaries.

pg_hba.conf can appear in many other places depending on how Pg was installed. The standard location is pg_hba.conf within the data_directory of the database (which could be in /home, /var/lib/pgsql, /var/lib/postgresql/[version]/, /opt/postgres/, etc etc etc) but users and packagers can put it wherever they like. Unfortunately.

The only valid ways find pg_hba.conf is to ask a running PostgreSQL instance where it's pg_hba.conf is, or ask the sysadmin where it is. You can't even rely on asking where the datadir is and parsing postgresql.conf because an init script might passed a param like -c hba_file=/some/other/path when starting Pg.

What you want to do is ask PostgreSQL:

SHOW hba_file;

This command must be run on a superuser session, so for shell scripting you might write something like:

psql -t -P format=unaligned -c 'show hba_file';

and set the environment variables PGUSER, PGDATABASE, etc to ensure that the connection is right.

Yes, this is somewhat of a chicken-and-egg problem, in that if the user can't connect (say, after screwing up editing pg_hba.conf) you can't find pg_hba.conf in order to fix it.

Another option is to look at the ps command's output and see if the postmaster data directory argument -D is visible there, e.g.

ps aux  | grep 'postgres *-D'

since pg_hba.conf will be inside the data directory (unless you're on Debian/Ubuntu or some derivative and using their packages).

If you're targeting specifically Ubuntu systems with PostgreSQL installed from Debian/Ubuntu packages it gets a little easier. You don't have to deal with hand-compiled-from-source Pg that someone's initdb'd a datadir for in their home dir, or an EnterpriseDB Pg install in /opt, etc. You can ask pg_wrapper, the Debian/Ubuntu multi-version Pg manager, where PostgreSQL is using the pg_lsclusters command from pg_wrapper.

If you can't connect (Pg isn't running, or you need to edit pg_hba.conf to connect) you'll have to search the system for pg_hba.conf files. On Mac and Linux something like sudo find / -type f -name pg_hba.conf will do. Then check the PG_VERSION file in the same directory to make sure it's the right PostgreSQL version if you have more than one. (If pg_hba.conf is in /etc/, ignore this, it's the parent directory name instead). If you have more than one data directory for the same PostgreSQL version you'll have to look at database size, check the command line of the running postgres from ps to see if it's data directory -D argument matches where you're editing, etc. https://askubuntu.com/questions/256534/how-do-i-find-the-path-to-pg-hba-conf-from-the-shell/256711

tayfun Kılıç
  • 2,042
  • 1
  • 14
  • 11
3

Many of the other answers pertain to settings in the various config files, and the ones pertaining to the pg_hba.conf do apply and are 100% correct. However, make sure you are modifying the correct config files.

As others have mentioned the config file locations can be overridden with various settings inside the main config file, as well as supplying a path to the main config file on the command line with the -D option.

You can use the following command while in a psql session to show where your config files are being read (assuming you can launch psql). This is just a troubleshooting step that can help some people:

select * from pg_settings where setting~'pgsql';  

You should also make sure that the home directory for your postgres user is where you expect it to be. I say this because it is quite easy to overlook this due to the fact that your prompt will display '~' instead of the actual path of your home directory, making it not so obvious. Many installations default the postgres user home directory to /var/lib/pgsql.

If it is not set to what it is supposed to be, stop the postgresql service and use the following command while logged in as root. Also make sure the postgres user is not logged into another session:

usermod -d /path/pgsql postgres

Finally make sure your PGDATA variable is set correctly by typing echo $PGDATA, which should output something similar to:

/path/pgsql/data

If it is not set, or shows something different from what you expect it to be, examine your startup or RC files such as .profile or .bash.rc - this will vary greatly depending on your OS and your shell. Once you have determined the correct startup script for your machine, you can insert the following:

export PGDATA=/path/pgsql/data

For my system, I placed this in /etc/profile.d/profile.local.sh so it was accessible for all users.

You should now be able to init the database as usual and all your psql path settings should be correct!

Bill
  • 89
  • 9
3

If you are facing this issue with rails and you know that you already have created that user-name with password along with correct rights then you just need to put following at the end of your database.yml file.

host: localhost

overall file will look like below

development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: root
  password: admin
  host: localhost

You do not need to touch you pg_hba.conf file at all. Happy coding

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
Mani
  • 2,391
  • 5
  • 37
  • 81
2

My issue was that I did not type any server. I thought it is a default because of placeholder but when I typed localhost it did work.

Epsilon47
  • 768
  • 1
  • 13
  • 28
2

If you are trying to locate this file in Cloud 9, you can do

sudo vim /var/lib/pgsql9/data/pg_hba.conf

Press I to edit/insert, press ESC 3 times and type :wq will save the file and quit

Marshall
  • 88
  • 6
2

In my case, I was not even able to edit or see the content of pg_hba.conf file.

What worked was:

/etc/postgresql/14/main$ sudo vi pg_hba.conf

Vi editor with sudo permission.

Singh
  • 504
  • 4
  • 15
1

On CentOS 7, PG 10, the file path is

/var/lib/pgsql/10/data/pg_hba.conf
Ravi Kumar Gupta
  • 1,698
  • 2
  • 22
  • 38
0

This error was misleading for me. I am running pg in docker with mounted ports. Adding -h <HostIp> -p <HostPort> to the psql command resolved the issue.

psql -U <User> -d <DB> -W -h <HostIp> -p <HostPort>

Shubham Jain
  • 876
  • 1
  • 9
  • 17
0

you dont need to edit anything, just use this command:

$psql -U postgres -h 127.0.0.1 -d [your_db]

cheers

Rozy Mahsun
  • 205
  • 2
  • 7