PostgreSQL: Ways to Reset Root Password on Ubuntu

Updated: January 4, 2024 By: Guest Contributor Post a comment

Introduction

At times, it becomes necessary to reset the PostgreSQL root password – particularly when the password is lost or forgotten. For Ubuntu users, there are multiple ways to reset the PostgreSQL server’s root password. This guide discusses various methods and provides step-by-step instructions.

Solution 1: Using the pg_pass file

This method involves editing the pg_pass file, which is used by Postgres to store passwords in a non-plain text manner. It can be used to automatically provide a password during connection to a particular database:

  • Edit the pg_pass file and insert the new desired credentials.
  • Use these credentials to log in and change the password via SQL command.

Commands to run:

echo "*:5432:*:postgres:newpassword123" > ~/.pgpass
chmod 600 ~/.pgpass
psql -h localhost -U postgres
ALTER USER postgres WITH PASSWORD 'newpassword123';

Advantages: This method provides a quick way of logging in without prompting for the initial password.

Limitations: Saving passwords in a file could be a potential security risk and it may be necessary to remove this file after use.

Solution 2: Alter User Command in SQL

If you have access to the PostgreSQL command line with another superuser account, you can change the root user’s password directly with a SQL command:

  • Login as a different superuser.
  • Run a SQL command to alter the root user’s password.

Commands to run in your terminal:

psql -U other_superuser -d template1
ALTER USER postgres PASSWORD 'newpassword123';

Advantages: This method directly changes the password for the root account without going through an intermediary file, making it secure.

Limitations: It requires access to another superuser account.

Solution 3: Service Restart with Init File

By modifying the PostgreSQL configuration to include an init file on restart, one can set a new password without needing to provide the current one. This approach allows you to create a file containing the SQL command to reset the password which PostgreSQL will run on start-up:

  • Create an init file with the new password SQL command.
  • Modify the PostgreSQL configuration to use this file on restart.
  • Restart PostgreSQL, then remove the init file setting.

Commands:

echo "ALTER USER postgres PASSWORD 'newpassword123';" > /tmp/pg_resetpass.conf
chmod 700 /tmp/pg_resetpass.conf
echo "local all postgres ident" > /etc/postgresql/12/main/pg_hba.conf
systemctl restart postgresql
psql -U postgres
ALTER USER postgres WITH PASSWORD 'newpassword123';

Advantages: Especially useful if all other superuser account passwords are unknown.

Limitations: Requires restarting the PostgreSQL server, which might be unsuitable during peak times.

Conclusion

To wrap things up, each solution for resetting the PostgreSQL root password comes with its own set of advantages and limitations. The choice of method depends on your specific circumstances, such as whether you have access to other superusers or if the PostgreSQL server can be restarted without affecting its operation. It’s crucial to follow security practices post-reset, like removing any init file configurations and ensuring the password is stored safely.