Reset PostgreSQL Superuser Password on Linux

Reset PostgreSQL Superuser Password on Linux

One fundamental aspect of database security is ensuring that access credentials, especially those of the superuser, are kept confidential and regularly updated. During PostgreSQL installation, a postgres superuser is typically created on most Linux distributions. If you forgot the superuser password, you can reset it by using a simple command.

This tutorial explains how to reset PostgreSQL superuser password on Linux. Testing has been done on Ubuntu.

Reset superuser password

Run the following command to access the PostgreSQL shell as the superuser (postgres) and change the password for the postgres superuser:

sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'pwd123';"

Don't forget to replace pwd123 with desired strong password.

Reset superuser password (Docker)

PostgreSQL can also be installed on Docker container. First, we need to find the container name. The following command allows getting a list of Docker containers:

docker ps

In most cases, the container is named postgresql. To change the postgres superuser password, run the following command:

docker exec -it postgresql psql -U postgres -c "ALTER USER postgres WITH PASSWORD 'pwd123';"

The 5 Comments Found

  1. Avatar
    Steven Reply

    Thank you for the tutorial. When running sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD 'pwd123';" I get the error message: "sudo: unknown user postgres" and "sudo: error initializing audit plugin sudoers_audit".

    Trying docker exec -it postgresql psql -U postgres -c "ALTER USER postgres WITH PASSWORD 'pwd123';" results in success message: "ALTER ROLE".

    • Avatar
      lindevs Reply

      The first command is applicable to users with a direct installation of PostgreSQL on their system. The error message says that the postgres user is not present, implying that PostgreSQL is likely not installed on your system.

      The second command is intended for users who have installed PostgreSQL within a Docker container. Your message indicates that superuser password has been changed successfully.

  2. Avatar
    Steven Reply

    In your previous tutorial, you set up Etherpad with:

    docker run -d --name=etherpad --restart=always --network=app-net \
        -p 8080:9001 \
        -e ADMIN_PASSWORD=pwd123 \
        -e DB_TYPE=postgres \
        -e DB_USER=postgres \
        -e DB_PASS=pwd123 \
        -e DB_NAME=etherpad \
        -e DB_HOST=postgresql \

    Do we have to change the config here too?

    I tried: docker exec -it etherpad psql -U postgres -c "ALTER USER postgres WITH PASSWORD 'pwd123';" but it throw the error: "OCI runtime exec failed: exec failed: unable to start container process: exec: "psql": executable file not found in $PATH: unknown"

    • Avatar
      lindevs Reply

      The docker exec -it etherpad psql ... command fails because PostgreSQL isn't installed in the Etherpad container; it's in a separate container. To fix this, update the superuser password in the PostgreSQL container and relaunch the Etherpad container with the new password using the DB_PASS parameter.

  3. Avatar
    Steve Reply

    Sidenote: If you want to change the admin password (the one that pops up when you want to visit the admin page), you change the line "password": "${ADMIN_PASSWORD:null}", inside the admin/settings to something like: "password": "mynewlongerpw123",

Leave a Comment

Cancel reply

Your email address will not be published.