288

I either forgot or mistyped (during the installation) the password to the default user of PostgreSQL. I can't seem to be able to run it, and I get the following error:

psql: FATAL:  password authentication failed for user "hisham"
hisham-agil: hisham$ psql

Is there a way to reset the password or how do I create a new user with superuser privileges?

I am new to PostgreSQL and just installed it for the first time. I am trying to use it with Ruby on Rails and I am running Mac OS X v10.7 (Lion).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hilarl
  • 6,570
  • 13
  • 48
  • 57
  • 1
    I cannot comment on answers yet, so have to do it this way. I did what SaiyanGirl said, however I still needed to enter a password, which was 'postgres' to log in, then I could reset the password – Pascale Dec 10 '16 at 08:28

16 Answers16

453
  1. Find the file pg_hba.conf. It may be located, for example, in /etc/postgresql-9.1/pg_hba.conf.

    cd /etc/postgresql-9.1/

  2. Back it up

    cp pg_hba.conf pg_hba.conf-backup

  3. Place the following line (as either the first uncommented line, or as the only one):

    For all occurrence of below (local and host) , except replication section if you don't have any it has to be changed as follow ,no MD5 or Peer authentication should be present.

    local all all trust

  4. Restart your PostgreSQL server (e.g., on Linux:)

    sudo /etc/init.d/postgresql restart

    If the service (daemon) doesn't start reporting in log file:

    local connections are not supported by this build

    you should change

    local all all trust

    to

    host all all 127.0.0.1/32 trust

  5. You can now connect as any user. Connect as the superuser postgres (note, the superuser name may be different in your installation. In some systems it is called pgsql, for example.)

    psql -U postgres

    or

    psql -h 127.0.0.1 -U postgres

    (note that with the first command you will not always be connected with local host)

  6. Reset the password ('replace my_user_name with postgres since you are resetting the postgres user)

    ALTER USER my_user_name with password 'my_secure_password';

  7. Restore the old pg_hba.conf file as it is very dangerous to keep around

    cp pg_hba.conf-backup pg_hba.conf

  8. Restart the server, in order to run with the safe pg_hba.conf file

    sudo /etc/init.d/postgresql restart

Further reading about that pg_hba file: 19.1. The pg_hba.conf File (official documentation)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arsen7
  • 12,522
  • 2
  • 43
  • 60
  • 1
    That's pretty much it. If possible, do this at the console and disconnect your machine from the network while you do this to avoid someone hacking it while you work. – Philip Couling Jun 01 '12 at 11:47
  • 13
    And then start `psql`? I'm still getting prompted for a password, and I do not know it – CodyBugstein Aug 24 '14 at 23:01
  • Worked great thanks! Note: Keep pgAdmin closed while following these steps. – Mujeeb Jul 19 '16 at 06:35
  • 9
    this answer did NOT work for me, after I do step 3, the service won´t start, I don´t know, maybe it´s the OS(W8), it just won´t. – Scaramouche Sep 12 '16 at 01:16
  • 5
    anyone for Windows? – Mahesha999 Nov 14 '16 at 08:02
  • 1
    @Mahesha999: it's the same for Windows. Except that you need `net stop ...` and `net start ...` or the Control Panel to stop and start the service (and you can not use `local` connections in Windows) –  Dec 10 '16 at 09:49
  • 1
    this command is not working for me local all all trust – vincet Mar 08 '17 at 16:05
  • 3
    Keep in mind order of entries in pg_hba.conf is important. If you add "local all all trust" to the end of the file, it'll not work as you expect, as previous records will be matched first. So put that at the top of the file to have what you expect. – Tagar Mar 14 '17 at 05:46
  • 13
    If you're on windows with this error, keep scrolling to @SaiyanGirl 's answer. Just modfiy the METHOD columns of the existing entries to 'trust', then change it back when you're done – sean.hudson Mar 28 '17 at 19:02
  • `psql -U postgres` will simply log in if not then you can use password that you have provided at installation time. – Bimlesh Sharma Jun 21 '17 at 16:39
  • 1
    I also had to add `local all postgres trust` to `/etc/postgresql/9.5/mainpg_hba.conf`. – Kris Sep 25 '17 at 08:56
  • 2
    You should comment all other lines or replace all `md5` with `trust`. – Alex78191 Mar 20 '18 at 09:59
  • This failed for me until I finally set the IPv6 from md5 to trust. I had thought it was ignoring that and only using IPv4. Apparently not. Perhaps removing the IPv6 line entirely would also have worked. – MickeyfAgain_BeforeExitOfSO Apr 29 '19 at 17:32
  • I found the `pg_hba.conf` file in `/Library/PostgreSQL/11/data` (MacOS v.10 and postgress v.11) – Ahmed Soliman Jul 09 '19 at 17:23
  • I found that the easiest way to find the file for mac is `find / -name 'pg_hba.conf' 2>/dev/null`. – Wit Jul 28 '19 at 02:50
  • I'm being asked for postgres password at step 2, same password ive forgotten, how do I go about it now? – Leslie Nyahwa Apr 29 '20 at 17:42
  • LOL, I've just spent an extra 30 minutes on this because I forgot to add a semicolon to the end of the SQL query – Fido Aug 12 '21 at 09:32
  • Just instead of `md5` need set `scram-sha-256` - for Postgres 14 / Ubuntu 22 – Oleksandr Bratashov Sep 08 '22 at 20:19
  • @CodyBugstein and anyone who is still prompted to enter password, I have solved it just by closing putty (I am accessing ubuntu using putty) and reconnecting. – Galilo Galilo Dec 25 '22 at 09:37
147

When connecting to PostgreSQL from the command line, don't forget to add -h localhost as a command line parameter. If not, PostgreSQL will try to connect using PEER authentication mode.

The below shows a reset of the password, a failed login with PEER authentication and a successful login using a TCP connection.

# sudo -u postgres psql

could not change directory to "/root"
psql (9.1.11)
Type "help" for help.

postgres=# \password
Enter new password:
Enter it again:
postgres=# \q

Failing:

# psql -U postgres -W

Password for user postgres:
psql: FATAL:  Peer authentication failed for user "postgres"

Working with -h localhost:

# psql -U postgres -W  -h localhost

Password for user postgres:
psql (9.1.11)
SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)
Type "help" for help.

postgres=#
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SaeX
  • 17,240
  • 16
  • 77
  • 97
88

The pg_hba.conf (C:\Program Files\PostgreSQL\9.3\data) file has changed since these answers were given. What worked for me, in Windows, was to open the file and change the METHOD from md5 to trust:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

Then, using pgAdmin III, I logged in without using a password and changed user postgres's password by going to menu FileChange Password.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SaiyanGirl
  • 16,376
  • 11
  • 41
  • 57
  • 3
    This doesn't work because PgAdmin still asks for the current password... I reset to 'trust' and restarted PgAdmin. Still can't reset without typing in the CURRENT password... – gene b. Nov 15 '17 at 15:11
  • 2
    Once you change the method to `trust` you can follow [this](https://www.postgresql.org/docs/8.0/static/sql-alteruser.html) and alter password in cmd – Gilad Green Feb 17 '18 at 08:38
  • 1
    **Interesting note:** Running PostgreSQL Server 13 on Windows, even after making the aforementioned changes, the server will still prompt for a password when trying to connect (when using pgAdmin). You can just hit "OK" (w/o specifying a password) and the server will let you in. I did not anticipate being prompted. – TMcManemy Sep 01 '20 at 13:57
  • Just to add to the above interesting note, I had to click "Cancel"; clicking "Okay" just got another demand for a password. And whenever I tried to actually do anything, I got a further demand, so it was not much help. – F2Andy Jun 22 '21 at 08:49
  • 1
    In my case the file was in `C:\Users\user\Documents\PostgreSQL\13\data`. – Ledorub Mar 24 '22 at 00:21
  • Thanks this worked for me, windows 11 user here. I logged into PgAdmin and selected the database – aero8991 Apr 22 '22 at 00:15
27
For Windows (what has helped me):

This is the document I am referring to: How can I reset a PostgreSQL password?

  1. Open your cmd and go to C:\Program Files\PostgreSQL\12\data. This is usually the right path. You might have it stored somewhere else. Note that, if you have a different PostgreSQL version, there will be a different number. That doesn't matter.

  2. Find a pg_hba.conf file and copy it to somewhere else (that way you will have an unmodified version of this file, so you will be able to look at it after we make some changes)

  3. Open the pg_hba.conf file (not the backup, but the original)

  4. Find the multiple lines that start with host near the bottom of the file:

    host    all             all             127.0.0.1/32            md5
    
    host    all             all             ::1/128                 md5
    
    host    replication     all             127.0.0.1/32            md5
    
    host    replication     all             ::1/128                 md5
    
  5. Replace md5 with trust:

    host    all             all             127.0.0.1/32            trust
    
    host    all             all             ::1/128                 trust
    
    host    replication     all             127.0.0.1/32            trust
    
    host    replication     all             ::1/128                 trust
    
  6. Close this file

  7. Go to your search bar on windows and open Services app. Find postgres and restart it.

    Picture of services app

  8. Write cd.. in cmd and then cd bin. Your path should be C:\Program Files\PostgreSQL\12\bin

  9. Enter: psql -U postgres -h localhost

  10. Enter: ALTER USER postgres with password '<your new password>';Make sure that you include ; at the end “ALTER ROLE” should be displayed as an indication that the previous line was executed successfully

  11. Open original pg_hba.conf file and change back from trust to md5

  12. Restart the server with Services app as before

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vito Farina
  • 271
  • 3
  • 2
21
  1. Edit the file /etc/postgresql/<version>/main/pg_hba.conf and find the following line:

    local  all  postgres  md5
    
  2. Edit the line and change md5 at the end to trust and save the file

  3. Reload the postgresql service

    sudo service postgresql reload
    
  4. This will load the configuration files. Now you can modify the postgres user by logging into the psql shell

    psql -U postgres
    
  5. Update the postgres user's password

    alter user postgres with password 'secure-passwd-here';
    
  6. Edit the file /etc/postgresql/<version>/main/pg_hba.conf and change trust back to md5 and save the file

  7. Reload the postgresql service

    sudo service postgresql reload
    
  8. Verify that the password change is working

    psql -U postgres -W
    
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ray Hunter
  • 15,137
  • 5
  • 53
  • 51
21

I was just having this problem on Windows 10 and the issue in my case was that I was just running psql and it was defaulting to trying to log in with my Windows username ("Nathan"), but there was no PostgreSQL user with that name, and it wasn't telling me that.

So the solution was to run psql -U postgres rather than just psql, and then the password I entered at installation worked.

Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
17

Just a note: On Linux, you can simply run sudo su - postgres to become the postgres user and from there change what is required using psql.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel
  • 199
  • 1
  • 4
8

For a Windows user for the latest PostgreSQL version (greater than 10):

Go to your PostgreSQL installation location, and search for pg_hba.conf, you will find it in ..\postgres\data\pg_hba.conf.

Open that file with Notepad, and find this line:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
#..

Change the method from *md5* to *trust*:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# ...

Now go to your SQL shell (PSQL) and leave everything blank,

Server [localhost]:
Database [postgres]:
Port [8000]:
Username [postgres]:

It will not ask for a password this time, and you will be logged in,

Now run this line:

  `ALTER USER yourusername WITH SUPERUSER`

Now you can leave the shell with \q.

Again, go to the file pg_hba.conf and change METHOD from trust to md5 again, and save it.

Now log in with your new user and password, and you can check \du for its attributes.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bidhan Majhi
  • 1,320
  • 1
  • 12
  • 25
3

If you are running PostgreSQL on macOS, try these:

1. Edit the pg_hba.conf file

sudo vi /Library/PostgreSQL/9.2/data/pg_hba.conf and Change the "md5" method for all users to "trust" near the bottom of the file

2. Find the name of the postgres service

ls /Library/LaunchDaemons Look for postgresql

3. Restart the postgresql service

sudo launchctl stop com.edb.launchd.postgresql-9.2

sudo launchctl start com.edb.launchd.postgresql-9.2 (com.edb.launchd.postgresql-9.2 should be name postgresql service from step 3)

4. Start a psql session as postgres psql -U postgres (shouldn't ask for password because of 'trust' setting)

5. Reset password in the psql session by typing:

    ALTER USER postgres with password 'secure-new-password';

6. Edit the pg_hba.conf file Switch it back to 'md5'

8. Restart services again

David
  • 3,843
  • 33
  • 36
2

For a Windows installation, a Windows user is created. And "psql" uses this user for connection to the port. If you change the PostgreSQL user's password, it won't change the Windows one. The command line just below works only if you have access to the command line.

Instead, you could use the Windows GUI application "c:\Windows\system32\lusrmgr.exe". This application manages users created by Windows. So you can now modify the password.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cpunky
  • 21
  • 1
2

I did this to resolve the same problem:

Open the pg_hba.conf file with the gedit editor from the terminal:

sudo gedit /etc/postgresql/9.5/main/pg_hba.conf

It will ask for a password. Enter your admin login password. This will open gedit with the file. Paste the following line:

host  all   all  127.0.0.1/32  trust
just below -

# Database administrative login by Unix domain socket

Save and close it.

Close the terminal, open it again and run this command:

psql -U postgres

You will now enter the psql console.

Now change the password by entering this:

ALTER USER [your preferred user name] with password '[desired password]';

If it says the user does not exist then instead of ALTER, use CREATE.

Lastly, remove that certain line you pasted in pg_hba and save it.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44
1
sudo -u postgres psql

ALTER USER user_name WITH PASSWORD 'new_password';
desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • Please read "[answer]" and "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code. – the Tin Man May 24 '23 at 05:06
0

If you are on Windows you can just run

net user postgres postgres

And log in in PostgreSQL with postgres/postgres as the user/password.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Follow step 1 on the best answer.

Here is my addition if you use the Windows operating system. Follow only step 1, and then open pgAdmin or postgres on web and click on file on the top nav. Click on reset layout, and finally reload the application. Whatever password you put should work. I used 1234.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

I didn't manage to find the file pg_hba.conf in the folder C:\Program Files\PostgreSQL\14\data, because there is not a folder data at all.

I solved the problem by creating a new user using pgAdmin and gave it super system administrator rights.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Barabas
  • 912
  • 8
  • 19
-1

Add the below line to your pg_hba.conf file. Which will be present in the installation directory of PostgreSQL

hostnossl    all          all            0.0.0.0/0  trust

It will start working.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131