How to change a user Password in PostgreSQL?
Changing a user's password in PostgreSQL is straightforward and can be done using the ALTER USER or ALTER ROLE command. Here’s a guide on how to change a PostgreSQL user's password using SQL commands and psql.
1. Using ALTER USER Command
The ALTER USER command allows you to modify attributes of a PostgreSQL user, including setting a new password.
Syntax:
ALTER USER username WITH PASSWORD 'new_password';
Example Code:
-- Change password for a specific user
ALTER USER my_user -- Specify the username you want to modify
WITH PASSWORD 'new_password123'; -- Set the new password (replace with actual password)
Explanation:
2. Using ALTER ROLE Command
ALTER ROLE is similar to ALTER USER and can also be used to set a new password for a user.
Syntax:
ALTER ROLE username WITH PASSWORD 'new_password';
Example Code:
-- Change password using ALTER ROLE
ALTER ROLE my_user -- Specify the username for password change
WITH PASSWORD 'new_password123'; -- Set the new password (replace with actual password)
Explanation:
3. Using psql Command-Line Interface
If you are using the PostgreSQL psql command-line tool, you can run either ALTER USER or ALTER ROLE commands directly.
Example Code:
-- Access the psql command-line interface
psql -U postgres -d your_database
-- Change the user password inside psql
ALTER USER my_user WITH PASSWORD 'new_password123';
Explanation:
Important Notes
All PostgreSQL Questions, Answers, and Code Snippets Collection.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4