How can I change a PostgreSQL user password?
Categories:
How to Change a PostgreSQL User Password

Learn the essential methods for securely changing a PostgreSQL user's password, covering both SQL commands and command-line utilities for different scenarios.
Managing user credentials is a fundamental aspect of database security. In PostgreSQL, changing a user's password is a straightforward process, but it's crucial to understand the different methods available and when to use each. This article will guide you through the various ways to update a PostgreSQL user's password, ensuring your database remains secure.
Understanding PostgreSQL User Authentication
PostgreSQL uses roles to manage database access and permissions. A user is essentially a role with the ability to log in. Passwords are an integral part of this authentication mechanism. When you change a user's password, you are updating the credential associated with that specific role. It's important to choose strong, unique passwords and rotate them regularly as part of a robust security policy.
flowchart TD A[Start] --> B{User Needs Password Change?} B -- Yes --> C{Is User Logged In?} C -- Yes --> D[ALTER USER SQL Command] C -- No --> E{Can Connect as Superuser?} E -- Yes --> F[ALTER USER SQL Command (Superuser)] E -- No --> G[psql Utility (Superuser)] D --> H[Password Changed] F --> H G --> H B -- No --> I[End]
Decision flow for changing a PostgreSQL user password.
Method 1: Using the ALTER USER SQL Command
The most common and recommended way to change a PostgreSQL user's password is by using the ALTER USER
SQL command. This method can be executed directly within a psql
session or any SQL client connected to your PostgreSQL database. You can change your own password or, if you have superuser privileges, change another user's password.
To change your own password, assuming you are logged in as the user whose password you want to change:
ALTER USER current_user WITH PASSWORD 'new_strong_password';
Changing your own PostgreSQL user password.
If you are a superuser (e.g., postgres
user) and need to change another user's password:
ALTER USER username WITH PASSWORD 'another_new_strong_password';
Changing another user's password as a superuser.
'new_strong_password'
with a genuinely strong, unique password. Consider using a password manager to generate and store complex passwords.Method 2: Using the \password Command in psql
The psql
command-line utility provides a convenient shorthand for changing passwords using the \password
meta-command. This is particularly useful for interactive sessions.
To change your own password using \password
:
psql -U your_username -d your_database
\password
Changing your own password interactively in psql.
After typing \password
, psql
will prompt you to enter and confirm the new password. If you are a superuser and want to change another user's password:
psql -U postgres -d your_database
\password target_username
Changing another user's password as a superuser using \password.
ALTER USER ... WITH PASSWORD '...'
, the password is sent as plain text in the SQL command. While this is generally secure over encrypted connections, be cautious in insecure environments. The \password
command in psql
prompts for the password separately, which can be slightly more secure as it avoids the password appearing in shell history or logs directly.Method 3: Changing Password for a Non-Existent User (Creating with Password)
While not strictly 'changing' a password, if you need to create a new user with a password or reset a password for a user that might have been dropped and recreated, you can do so directly during user creation or modification.
CREATE USER new_user WITH PASSWORD 'initial_secure_password';
-- Or, if the user exists and you want to ensure a password is set/reset
ALTER USER existing_user WITH PASSWORD 'reset_secure_password';
Creating a user with a password or resetting an existing user's password.
ident
, peer
, trust
, and GSSAPI
. If your pg_hba.conf
is configured to use one of these methods for a specific connection, a password might not be required or used for authentication.Best Practices for Password Management
Beyond just knowing how to change a password, adopting good password management practices is vital for database security:
1. Use Strong, Unique Passwords
Always use passwords that are long, complex, and unique for each user and service. Avoid common words, personal information, or easily guessable patterns.
2. Rotate Passwords Regularly
Implement a policy for regular password rotation, especially for administrative accounts. This reduces the risk of compromised credentials being used over extended periods.
3. Limit Superuser Access
Only grant superuser privileges when absolutely necessary. For daily operations, use roles with the minimum required permissions.
4. Secure Connection Strings
Ensure that connection strings and client applications do not hardcode passwords. Use environment variables, configuration files, or secret management systems.
5. Monitor Authentication Logs
Regularly review PostgreSQL logs for failed login attempts, which can indicate brute-force attacks or unauthorized access attempts.