How to Access PostgreSQL Default Superuser (postgres) and Reset Password After Installation on Ubuntu


3 views

After a fresh PostgreSQL installation on Ubuntu (or most Linux distributions), the default superuser account is:


Username: postgres
Password: [none by default]

This account is created automatically during installation with full administrative privileges but has no password initially. Depending on your PostgreSQL version and installation method, you might need to configure access before using it.

There are two primary methods to gain initial access:

Method 1: Using Peer Authentication (Local Login)


sudo -u postgres psql

This works because PostgreSQL by default uses "peer" authentication for local connections, matching the system username to the PostgreSQL username.

Method 2: Setting Initial Password


# First access the PostgreSQL prompt
sudo -u postgres psql

# Then set password for postgres user
\password postgres
Enter new password: 
Enter it again: 

If you prefer not to use the default postgres account:


sudo -u postgres createuser --interactive --pwprompt
Enter name of role to add: myadmin
Shall the new role be a superuser? (y/n) y
Password: 

If you encounter authentication errors, check these configuration files:

pg_hba.conf (usually in /etc/postgresql/[version]/main/)


# Local connections (add or modify)
local   all             postgres                                peer
# OR for password auth
local   all             postgres                                md5

postgresql.conf (ensure listen_addresses isn't restricted):


listen_addresses = 'localhost'

For PostgreSQL 15+, you may need to explicitly grant connection permission:


ALTER ROLE postgres WITH LOGIN;

Remember to restart PostgreSQL after configuration changes:


sudo service postgresql restart

When you install PostgreSQL on Ubuntu or other Linux distributions, the system automatically creates a default superuser account named postgres. This account has full administrative privileges but typically doesn't have a password set initially.

You can access the PostgreSQL server using peer authentication (local connections) with:

sudo -u postgres psql

This command switches to the postgres system user and launches the psql client. No password is required because it uses peer authentication.

To add password authentication, first access the PostgreSQL prompt:

sudo -u postgres psql

Then set a password:

ALTER USER postgres WITH PASSWORD 'your_secure_password';

If you prefer to create a separate superuser account:

CREATE USER your_username WITH SUPERUSER CREATEDB CREATEROLE LOGIN PASSWORD 'your_password';

To enable password authentication for remote connections, edit /etc/postgresql/8.4/main/pg_hba.conf:

# Change this line:
local   all             postgres                                peer

# To:
local   all             postgres                                md5

Then restart PostgreSQL:

sudo service postgresql-8.4 restart

If you encounter authentication problems:

  • Verify the PostgreSQL service is running: sudo service postgresql status
  • Check authentication logs: tail -f /var/log/postgresql/postgresql-8.4-main.log
  • Confirm the postgres user exists: sudo -u postgres id