Setting and Updating User Passwords in PostgreSQL
Setting and updating user passwords in PostgreSQL is good for securing our database from unauthorized access. By managing user credentials and configuring authentication methods we can enhance control over user roles and ensure data security.
In this article, we will discuss how to set and update passwords for users in the best ways to secure information by embracing the best practices on database security.
Setting and Updating User Passwords in PostgreSQL
A default password for the superuser account (usually named Postgres) helps prevent unauthorized access. It also protects sensitive data. Managing user credentials properly enhances control over roles and privileges in the database.
This process provides security and will manage the user credentials to be correctly administered. Using effective control of roles and privileges within the database.
Prerequisites
Before you begin, ensure the following:
- PostgreSQL is installed on our machine.
- We have access to our user account with the necessary permissions (e.g., postgres or any superuser).
- We can access the PostgreSQL Command Line Interface (CLI).
Steps to Setting and Updating User Passwords in PostgreSQL
Step 1: Accessing PostgreSQL CLI
Initially, open the PostgreSQL Command Line Interface (psql). For this, we just need to type this command below in your terminal or command prompt:
Here, Postgres is the default superuser. The prompt asks for a password for the postgres user.
psql -U postgres
Step 2: Viewing All PostgreSQL Users
This command will list the users and their roles in the system. To print out all users in PostgreSQL, we can run the following query:
\du
Step 3: Creating a New User (Optional)
If we already have an existing user or do not have an existing user then we can simply create one executing the following command. Replace username with the username you want to assign and password with the password that you are going to assign.
CREATE USER username WITH PASSWORD 'password';
Step 4: Changing a User's Password
To change the password of an existing user, including the postgres user, we can use the ALTER USER command. Replace username with the user we want to alter and new_password with the new password you wish.
ALTER USER username WITH PASSWORD 'new_password';
Example: This makes the default postgres user password securepassword123.
ALTER USER postgres WITH PASSWORD 'securepassword123';
Step 5: Saving and Exiting
Once you have entered the password, we will quit the psql interface:
\q
Step 6: Verifying Password Change
To verify that the password change has been accepted, try logging in with the new credentials. The -W flag requires the password. Now Type the new password when prompted to confirm the change.
psql -U username -W
Step 7: Configure pg_hba.conf (Optional)
From time to time we might need to modify PostgreSQL's authentication controls to allow authentication via a password. This step requires modification of pg_hba.conf.
- Open the pg_hba.conf file located in our PostgreSQL data directory:
sudo nano /etc/postgresql/<version>/main/pg_hba.conf
- Change the method of authentication to md5 or scram-sha-256, intended for password authentication. For example:
# TYPE DATABASE USER ADDRESS METHODhost all all 127.0.0.1/32 md5
- Save the file and restart PostgreSQL to apply changes:
sudo systemctl restart postgresql
Conclusion
Updating passwords and following secure authentication practices in PostgreSQL strengthens database protection. Regular password updates, combined with proper role management, ensure your PostgreSQL environment remains secure and resilient to threats.