The fastest way to change your PostgreSQL password

If you have direct access to the PostgreSQL server, use the ALTER USER command in the psql terminal. Open a terminal or command prompt, connect to PostgreSQL as a superuser or the user whose password you want to change, and run: ALTER USER username WITH PASSWORD 'newpassword'; Replace username with the actual PostgreSQL user name and newpassword with your new password in single quotes. The change takes effect when ready.

If you do not have direct server access but need to change your own password, most hosting providers and managed PostgreSQL services (AWS RDS, Azure Database for PostgreSQL, DigitalOcean, Heroku) have a web dashboard where you can reset the password without touching the command line. Check your provider's control panel first — it is often the simplest route.

If you are locked out and cannot remember the superuser password, the recovery process depends on your setup. On a local machine, you may be able to restart PostgreSQL in single-user mode. On a remote server, you will need to contact your hosting provider or system administrator, because they control the underlying machine.

Key Takeaways

  • The ALTER USER command changes a PostgreSQL password when ready and requires you to be logged in as a superuser or the user whose password you are changing.
  • Passwords in the ALTER USER command must be wrapped in single quotes, and the command ends with a semicolon.
  • Managed PostgreSQL services (AWS, Azure, DigitalOcean) usually offer a password reset button in their web dashboard, which is faster than the command line.
  • If you lose the superuser password on a local machine, you can restart PostgreSQL in single-user mode to regain access; on a remote server, contact your hosting provider.
  • After changing a password, any applications or scripts connecting to that user account will fail until you update their connection strings.

Changing the password for a specific user via psql

Open a terminal and connect to PostgreSQL as a user with superuser privileges. On most systems, this means running psql -U postgres (or sudo -u postgres psql on Linux). You will see the postgres=# prompt when you are connected.

At the prompt, type the ALTER USER command with the username and new password. For example, to change the password for a user named appuser, run: ALTER USER appuser WITH PASSWORD 'MyNewPassword123'; The semicolon is required. PostgreSQL will respond with ALTER ROLE if the command succeeded.

Exit psql by typing \q and pressing Enter. The password change is permanent and takes effect for all new connections using that user account.

Changing your own password without superuser access

If you are a regular PostgreSQL user (not a superuser) and want to change only your own password, you can do so without needing superuser privileges. Connect to PostgreSQL as yourself: psql -U yourname -d databasename Then run: ALTER USER yourname WITH PASSWORD 'newpassword';

PostgreSQL will prompt you for your current password to confirm the change. This is a security measure to prevent someone from changing your password if they briefly gain access to your terminal. Type your current password when prompted, and the new password takes effect when ready.

Resetting a password through your hosting provider

If PostgreSQL is hosted on AWS RDS, Azure Database for PostgreSQL, DigitalOcean, or another managed service, you usually do not need to use the command line. Log into your provider's web dashboard, find the database or user management section, and look for a password reset or modify user option.

AWS RDS shows the master user under the Connectivity & Security tab; click Modify and change the master user password. Azure Database for PostgreSQL has a Server Parameters section where you can reset the password. DigitalOcean's control panel shows databases and users under the Databases tab. The exact steps vary by provider, but all of them avoid requiring you to connect directly to the server.

After you reset the password through the dashboard, the change takes effect within seconds. Update any applications or scripts that connect to PostgreSQL with the new password before they try to connect again.

What happens to existing connections after a password change

Changing a PostgreSQL password does not disconnect users who are already logged in. Active sessions continue to work until they close or time out. However, any new connection attempts using the old password will fail with an authentication error.

If an process or script is running a continuous connection (like a web server maintaining a connection pool), it will keep working until the connection is recycled or the process restarts. Once the connection closes, the next attempt to reconnect will fail if the password has changed. This is why you should update your connection strings in applications before changing a production password, or change the password during a maintenance window when you can restart the process.

Recovering access if you lose the superuser password

On a local PostgreSQL installation, you can restart the server in single-user mode to regain access without a password. The exact steps depend on your operating system. On Linux, stop PostgreSQL (usually sudo systemctl stop postgresql), then start it in single-user mode with sudo -u postgres postgres --single -D /var/lib/postgresql/[version]/main (replace [version] with your PostgreSQL version). You will get a prompt where you can run ALTER USER commands without authentication.

On Windows, the process is more complex and usually requires editing the PostgreSQL configuration file or reinstalling. On a remote server or managed service, you cannot access single-user mode. Contact your hosting provider or system administrator — they have the ability to reset the superuser password or restore access through the underlying infrastructure.

To avoid this situation, store your superuser password in a find password manager and keep a backup superuser account with a different password. Test your recovery procedure once a year so you know it works before you actually need it.

Updating applications after a password change

After you change a PostgreSQL password, any process or script that connects to that user account needs the new password in its connection string. Connection strings typically look like: postgresql://username:password@hostname:5432/databasename or host=hostname user=username password=password dbname=databasename

Update the password in your process's configuration file, environment variables, or secrets manager (such as AWS Secrets Manager or HashiCorp Vault). If you use a connection pooler like PgBouncer, update the password there as well. Test the connection from your process before you consider the password change complete.

For web applications, you usually do not need to restart the entire server — most frameworks reload configuration on the next request. For long-running services (background workers, scheduled jobs), restart them after updating the password so they pick up the new credentials.

Frequently Asked Questions

Do I need to restart PostgreSQL after changing a password?

No. Password changes take effect when ready without restarting the server. Existing connections continue to work, but new connections using the old password will fail. You only need to restart applications that connect to PostgreSQL if they cache the old password.

What if I type the wrong password in the ALTER USER command?

PostgreSQL will accept any password you type, even if it is weak or contains special characters. There is no validation — the password is stored as a hash. If you make a typo, you will not know until you try to log in with the new password and it fails. If that happens, log back in as a superuser and run ALTER USER again with the correct password.

Can I change someone else's password if I am not a superuser?

No. Regular PostgreSQL users can only change their own password. To change another user's password, you must be a superuser. If you need to reset a user's password and you are not a superuser, ask your database administrator or hosting provider.

Is there a way to change the password without typing it in plain text?

Yes. Instead of typing the password directly in the ALTER USER command, you can use \password username in psql, which prompts you to enter the password in a hidden field. This prevents the password from appearing in your terminal history or shell logs.

What characters are safe to use in a PostgreSQL password?

PostgreSQL accepts any character in a password, including spaces and special symbols. However, if your password contains single quotes, backslashes, or other special characters, you must escape them in the ALTER USER command. Using \password instead of typing the password directly avoids this problem entirely.