How to Change User Password in PostgreSQL

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

Introduction

Changing a user password in PostgreSQL is a common task for database administrators that ensures access control and enhances security. In this tutorial, we’ll discuss several methods to change the user password in PostgreSQL, from basic commands to more advanced options.

Prerequisites

Before altering user passwords, ensure that you have administrative privileges to the PostgreSQL server and the necessary credentials to access the database. It’s also essential that you’ve installed PostgreSQL and can access the command-line interface (CLI), pgAdmin, or any other database management tool.

Changing Password Using SQL Command

To change the user’s password in PostgreSQL, you will primarily use the ALTER USER command. Here is a simple example:

ALTER USER myuser WITH PASSWORD 'newpassword';

This command changes the password for the user named ‘myuser’ to ‘newpassword’. Remember to replace ‘newpassword’ with a strong, secure password.

Using psql Command Line

You can execute the above command by logging into the PostgreSQL terminal with the following command:

psql -U username -d dbname

Once logged in, you can run the ALTER USER command directly.

Using pgAdmin

For those who prefer a graphical interface, pgAdmin can be used to change passwords as well. Right-click on the target user under ‘Login/Group Roles’, select ‘Properties’, and navigate to the ‘Definition’ tab to update the password.

Changing Password for Multiple Users

If you need to change passwords for multiple users, you can iterate through a list and use the ALTER USER command for each. For example:

DO
$
DECLARE
    user_record RECORD;
BEGIN
    FOR user_record IN SELECT * FROM pg_catalog.pg_user WHERE usename != 'postgres'
    LOOP
        EXECUTE 'ALTER USER ' || quote_ident(user_record.usename) || ' WITH PASSWORD ''' || 'newpassword' || '''';
    END LOOP;
END
$;

This code snippet excludes the ‘postgres’ superuser from the password reset operation.

Password Policies

It’s important to ensure that changed passwords comply with your organization’s password policies. PostgreSQL supports password complexity checks, expiration, and history through its contrib modules, like ‘passwordcheck’. Using these features requires additional server configuration.

Using Encrypted Passwords

For security purposes, it is advised to use encrypted passwords in PostgreSQL. The following SQL command sets an encrypted password for a user:

ALTER USER myuser WITH ENCRYPTED PASSWORD 'newpassword';

Note that PostgreSQL automatically uses encrypted passwords by default in later versions.

Expire Password Feature

PostgreSQL also allows you to expire passwords to enforce regular updates. After expiration, the user will be prompted to change their password:

ALTER USER myuser PASSWORD 'newpassword' VALID UNTIL 'YYYY-MM-DD';

The ‘VALID UNTIL’ clause specifies the expiration date of the password. It’s a good practice to set a reminder to update passwords before they expire to prevent lockout issues.

Command-line Password Change

You can also change a PostgreSQL user’s password from a bash script using ‘psql’. Below is an example of how this can be achieved:

echo "ALTER USER myuser WITH PASSWORD 'newpassword';" | psql -d mydatabase

This method executes the ALTER USER command as standard input for the psql client connected to ‘mydatabase’.

Advanced Password Management

In an advanced setup, you might be operating with a failover or replication system, where password changes need to be synchronized across servers, or you might be using a configuration management system like Ansible or Chef to manage your passwords. These scenarios require automation and additional error handling when changing passwords to ensure consistency and reduce manual errors.

Auditing Password Changes

Lastly, it’s good practice to have auditing procedures to track changes, especially for sensitive operations such as password updates. Ensure that you log administrative operations and review them periodically for unexpected changes or security breaches.

Conclusion

Changing a user password in PostgreSQL is a straightforward process, but one which holds significant importance for maintaining database security. The methods covered in this tutorial range from simple command-line executions to more complex scenarios requiring automation. With this knowledge, you can effectively manage user access and enhance your PostgreSQL database’s security posture.