This is a newbie-level recipe to reset the superuser password, which works on all fresh installations of PostgreSQL on Linux.
Go to the shell and switch user to postgres
(in user shell) sudo su - postgres
connect to the postgres
database as postgres
user
(in postgres shell) psql postgres postgres
now you can reset password of postgres
user
(in postgres psql) ALTER USER postgres PASSWORD 'newsecret';
quit psql
(in postgres psql) \q
quit postgres shell
(in postgres shell) exit
test connection with new password
(in user shell) psql -h localhost postgres postgres
Note on remote postgres servers
In step 1 above, you can use ssh
, kubectl exec
, aws ssm
or anything like that, if you have this kind of shell access.
Best Practice note
Above recipe (though it answers the OP question) is not a good practice. The best approach is:
Read and understand client auth -> https://www.postgresql.org/docs/current/client-authentication.html
Do not use postgres
database user (or any other superuser!) for applications/development. Create your own user instead. For the simplest setup, use this:
(in psql shell)
CREATE USER myapp PASSWORD 'secret';
CREATE DATABASE myapp;
ALTER DATABASE myapp OWNER TO myapp;
-- alternative if you want to keep default ownership:
-- GRANT ALL ON DATABASE myapp TO myapp;
This should be done instead of modifying postgres
user and/or postgres
database.
Note on Managed postgres solutions
This answer applies only for self-managed PostgreSQL, where you have superuser shell access. It will not work for managed solutions like Aurora, CloudSQL or alike - use cloud provider tools to reset db passwords in that case.
postgres
is allowed to log in without a password. See client authentication in the manual.