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).
asked Jun 1, 2012 at 7:14
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/
-
Back it up
cp pg_hba.conf pg_hba.conf-backup
-
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
-
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
-
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)
-
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';
-
Restore the old pg_hba.conf file as it is very dangerous to keep around
cp pg_hba.conf-backup pg_hba.conf
-
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)
answered Jun 1, 2012 at 7:42
Arsen7Arsen7
12.5k2 gold badges43 silver badges60 bronze badges
19
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=#
answered Feb 2, 2014 at 10:21
SaeXSaeX
17.3k16 gold badges77 silver badges97 bronze badges
1
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 File → Change Password.
answered Sep 19, 2014 at 22:26
SaiyanGirlSaiyanGirl
16.4k11 gold badges41 silver badges57 bronze badges
6
For Windows (what has helped me):
This is the document I am referring to: How can I reset a PostgreSQL password?
-
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. -
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)
-
Open the pg_hba.conf file (not the backup, but the original)
-
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
-
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
-
Close this file
-
Go to your search bar on windows and open Services app. Find postgres and restart it.
Picture of services app
-
Write cd.. in cmd and then cd bin. Your path should be
C:\Program Files\PostgreSQL\12\bin
-
Enter:
psql -U postgres -h localhost
-
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 -
Open original pg_hba.conf file and change back from trust to md5
-
Restart the server with Services app as before
answered Nov 15, 2020 at 22:14
Vito FarinaVito Farina
2713 silver badges2 bronze badges
1
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.
answered Jun 26, 2019 at 21:29
Nathan WailesNathan Wailes
10k7 gold badges59 silver badges98 bronze badges
0
-
Edit the file
/etc/postgresql/<version>/main/pg_hba.conf
and find the following line:local all postgres md5
-
Edit the line and change
md5
at the end totrust
and save the file -
Reload the postgresql service
sudo service postgresql reload
-
This will load the configuration files. Now you can modify the
postgres
user by logging into thepsql
shellpsql -U postgres
-
Update the
postgres
user’s passwordalter user postgres with password 'secure-passwd-here';
-
Edit the file
/etc/postgresql/<version>/main/pg_hba.conf
and changetrust
back tomd5
and save the file -
Reload the postgresql service
sudo service postgresql reload
-
Verify that the password change is working
psql -U postgres -W
answered Aug 17, 2017 at 2:55
Ray HunterRay Hunter
15.2k5 gold badges53 silver badges51 bronze badges
2
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.
answered Mar 12, 2018 at 12:58
DanielDaniel
1991 silver badge4 bronze badges
1
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.
answered Feb 3, 2019 at 13:40
Bidhan MajhiBidhan Majhi
1,3201 gold badge12 silver badges25 bronze badges
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
answered Mar 16, 2020 at 15:22
DavidDavid
3,86333 silver badges36 bronze badges
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.
answered Mar 21, 2017 at 15:31
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.
answered Nov 14, 2017 at 12:08
Taufiq RahmanTaufiq Rahman
5,6302 gold badges37 silver badges44 bronze badges
sudo -u postgres psql
ALTER USER user_name WITH PASSWORD 'new_password';
desertnaut
57.9k27 gold badges140 silver badges167 bronze badges
answered May 21 at 11:20
1
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.
answered Jun 7, 2016 at 16:36
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.
answered Jun 27, 2022 at 11:12
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.
answered Nov 23, 2022 at 15:31
BarabasBarabas
9208 silver badges20 bronze badges
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.
answered Feb 26, 2021 at 10:47
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).
asked Jun 1, 2012 at 7:14
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/
-
Back it up
cp pg_hba.conf pg_hba.conf-backup
-
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
-
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
-
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)
-
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';
-
Restore the old pg_hba.conf file as it is very dangerous to keep around
cp pg_hba.conf-backup pg_hba.conf
-
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)
answered Jun 1, 2012 at 7:42
Arsen7Arsen7
12.5k2 gold badges43 silver badges60 bronze badges
19
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=#
answered Feb 2, 2014 at 10:21
SaeXSaeX
17.3k16 gold badges77 silver badges97 bronze badges
1
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 File → Change Password.
answered Sep 19, 2014 at 22:26
SaiyanGirlSaiyanGirl
16.4k11 gold badges41 silver badges57 bronze badges
6
For Windows (what has helped me):
This is the document I am referring to: How can I reset a PostgreSQL password?
-
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. -
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)
-
Open the pg_hba.conf file (not the backup, but the original)
-
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
-
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
-
Close this file
-
Go to your search bar on windows and open Services app. Find postgres and restart it.
Picture of services app
-
Write cd.. in cmd and then cd bin. Your path should be
C:\Program Files\PostgreSQL\12\bin
-
Enter:
psql -U postgres -h localhost
-
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 -
Open original pg_hba.conf file and change back from trust to md5
-
Restart the server with Services app as before
answered Nov 15, 2020 at 22:14
Vito FarinaVito Farina
2713 silver badges2 bronze badges
1
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.
answered Jun 26, 2019 at 21:29
Nathan WailesNathan Wailes
10k7 gold badges59 silver badges98 bronze badges
0
-
Edit the file
/etc/postgresql/<version>/main/pg_hba.conf
and find the following line:local all postgres md5
-
Edit the line and change
md5
at the end totrust
and save the file -
Reload the postgresql service
sudo service postgresql reload
-
This will load the configuration files. Now you can modify the
postgres
user by logging into thepsql
shellpsql -U postgres
-
Update the
postgres
user’s passwordalter user postgres with password 'secure-passwd-here';
-
Edit the file
/etc/postgresql/<version>/main/pg_hba.conf
and changetrust
back tomd5
and save the file -
Reload the postgresql service
sudo service postgresql reload
-
Verify that the password change is working
psql -U postgres -W
answered Aug 17, 2017 at 2:55
Ray HunterRay Hunter
15.2k5 gold badges53 silver badges51 bronze badges
2
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.
answered Mar 12, 2018 at 12:58
DanielDaniel
1991 silver badge4 bronze badges
1
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.
answered Feb 3, 2019 at 13:40
Bidhan MajhiBidhan Majhi
1,3201 gold badge12 silver badges25 bronze badges
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
answered Mar 16, 2020 at 15:22
DavidDavid
3,86333 silver badges36 bronze badges
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.
answered Mar 21, 2017 at 15:31
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.
answered Nov 14, 2017 at 12:08
Taufiq RahmanTaufiq Rahman
5,6302 gold badges37 silver badges44 bronze badges
sudo -u postgres psql
ALTER USER user_name WITH PASSWORD 'new_password';
desertnaut
57.9k27 gold badges140 silver badges167 bronze badges
answered May 21 at 11:20
1
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.
answered Jun 7, 2016 at 16:36
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.
answered Jun 27, 2022 at 11:12
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.
answered Nov 23, 2022 at 15:31
BarabasBarabas
9208 silver badges20 bronze badges
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.
answered Feb 26, 2021 at 10:47
Забыли пароль учетной записи postgres в PostgreSQL? Выполнить сброс не сложно. Для этого необходимо выполнить пару манипуляций.
1. Правим файл pg_hba.conf
Находим файл в папке Data директории установки PostgreSQL. В Windows путь выглядит примерно так c:\Program Files\PostgreSQL\9.2.4-1.1C\data\
В этом файле нужно найти такие строчки
# IPv4 local connections: host all all 127.0.0.1/32 md5 host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5
Меняем md5 на trust.
2. Удаляем файл pgpass.conf
В Windows этот файл находится в c:\Users\Administrator\AppData\Roaming\postgresql\
Здесь хранится старый пароль от PostgreSQL. Простое изменение хранимого здесь пароля мне не помогло. Поэтому я его просто удалил.
3. Меняем пароль в pgAdmin
Запускаем pgAdmin и нам предлагается ввести пароль. Если отметить галочку сохранить, то пароль будет сохранен в pgpass.conf и больше программой запрашиваться не будет.
Чтобы обеспечить безопасность использования паролей необходимо вернуть алгоритм шифрования md5. Для этого в файле pg_hba.conf параметр trust обратно меняем на md5.
Для подключения на локальном компьютере к PostgreSQL с помощью psql, pg_dump в локальных адресах IPv4 127.0.0.1/32 и IPv6 ::1/128 значение trust нужно оставить.
Спасибо софт-сетап
Join @AdmNtsRu on Telegram
Смотрите также:
Забыли пароль учетной записи postgres в PostgreSQL? Выполнить сброс не сложно. Для этого необходимо выполнить пару манипуляций.
1. Правим файл pg_hba.conf
Находим файл в папке Data директории установки PostgreSQL. В Windows путь выглядит примерно так c:\Program Files\PostgreSQL\9.2.4-1.1C\data\
В этом файле нужно найти такие строчки
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
Меняем md5 на trust.
2. Удаляем файл pgpass.conf
В Windows этот файл находится в c:\Users\Administrator\AppData\Roaming\postgresql\
Здесь хранится старый пароль от PostgreSQL. Простое изменение хранимого здесь пароля мне не помогло. Поэтому я его просто удалил.
3. Меняем пароль в pgAdmin
Запускаем pgAdmin и нам предлагается ввести пароль. Если отметить галочку сохранить, то пароль будет сохранен в pgpass.conf и больше программой запрашиваться не будет.
Чтобы обеспечить безопасность использования паролей необходимо вернуть алгоритм шифрования md5. Для этого в файле pg_hba.conf параметр trust обратно меняем на md5.
Для подключения на локальном компьютере к PostgreSQL с помощью psql, pg_dump в локальных адресах IPv4 127.0.0.1/32 и IPv6 ::1/128 значение trust нужно оставить.
Summary: in this tutorial, we will show you step by step how to reset the password of the postgres
user in PostgreSQL.
For some reason, after installing PostgreSQL, you may forget the password of the postgres
user. In this case, you need to know how to reset the password to access to the PostgreSQL server.
PostgreSQL uses the pg_hba.conf
configuration file stored in the database data directory (e.g., C:\Program Files\PostgreSQL\12\data
on Windows) to control the client authentication. The hba
in pg_hba.conf
means host-based authentication.
To reset the password for the postgres
user, you need to modify some parameters in this configuration file, login as postgres
without a password, and reset the password.
The following steps show you how to reset a password for the postgres
user:
Step 1. Backup the pg_hba.conf
file by copying it to a different location or just rename it to pg_hba.conf.bk
Step 2. Edit the pg_dba.conf
file and change all local connections from md5
(or scram-sha-256
in a newer version) to trust
. By doing this, you can log in to the PostgreSQL database server without using a password.
# 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
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
Code language: PHP (php)
Step 3. Restart the PostgreSQL server. If you are on Windows, you can restart the PostgreSQL from Services:
Or run the following command from the window terminal:
pg_ctl -D "C:\Program Files\PostgreSQL\12\data" restart
Code language: JavaScript (javascript)
The "C:\Program Files\PostgreSQL\12\data"
is the data directory.
Step 4. Connect to PostgreSQL database server using any tool such as psql or pgAdmin:
psql -U postgres
PostgreSQL will not require a password to login.
Step 5. Execute the following command to set a new password for the postgres
user.
postgres=# ALTER USER postgres WITH PASSWORD 'new_password';
Code language: SQL (Structured Query Language) (sql)
Step 6. Restore the pg_dba.conf
file, restart the PostgreSQL database server and connect to the PostgreSQL database server with the new password.
In this tutorial, you have learned how to reset the password of the postgres
user.
Was this tutorial helpful ?