Home » Programming » Databases » PostgreSQL – Default Password/Resetting the Default Password

PostgreSQL – Default Password/Resetting the Default Password

This post will explain how user accounts by default work in PostgreSQL, and how you can change the default password.

PostgreSQL works a bit differently to other database servers like MySQL when it comes to the default behaviour of user accounts, so read on to find out how to manage access as the default PostgreSQL admin user.

PostgreSQL uses the system users

The default PostgreSQL configuration uses ident for authentication – this method uses the hosts operating systems usernames and compares them against the permissions stored for each database. This means that you must be logged in as the authorized user in the operating system to run command-line commands, including administrative tasks.

The Default PostgreSQL Use is called postgres

When you install PostgreSQL, a user called postgres is set up, with administrative rights. This is the default administrative account for PostgreSQL.

PostgreSQL Default Password

The postgres user explained above has no password by default! So, you will not be able to log in as them directly. You can impersonate the user as root by using the su command

sudo su postgres

This will allow you to execute commands as the postgres user without having to log in.

Resetting PostgreSQL Default Password

If you do for some reason need to log in as the postgres user directly, you can create a password (or change an existing password) for that account using the passwd command:

sudo passwd postgres

…or, if you want to add a password for the postgres user that is only used by PostgreSQL (and cannot be used to log in to the system), run the following commands:

sudo su postgres
psql

This will land you on the PostgreSQL command line, where you can enter the command:

ALTER USER postgres PASSWORD 'NEWPASSWORD';

To set the password

Adding a user to PostgreSQL Only

postgres=# CREATE USER user_name PASSWORD new_password;
postgres=# GRANT ALL ON ALL TABLES IN SCHEMA database_name TO user_name;

Of course, you’ll want more granular permissions in production – you can’t let your users access databases with admin rights – so check out our article on postgresql users and roles.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment