PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
Home / PostgreSQL Administration / How To Change The Password of a PostgreSQL User

How To Change The Password of a PostgreSQL User

Summary: in this tutorial, you will learn how to change the password for a user in PostgreSQL.

To change the password of a PostgreSQL user, you use the ALTER ROLE statement as follows:

1
2
3
ALTER ROLE username  
WITH PASSWORD 'password';
 

In this statement, to change the password of a user:

  • First, specify the username who you want to change the password.
  • Second, provide the new password wrapped within single quotes (‘).

For example, the following statement changes the password of the super user to secret123.

1
ALTER ROLE super WITH PASSWORD 'secret123';

Sometimes, you want to set the password valid until a date and time. In this case, you use the VALID UNTIL clause:

1
2
3
ALTER ROLE username
WITH PASSWORD 'new_password'
VALID UNTIL timestamp;

Note that if you omit the VALID UNTIL clause, the password will be valid for all time.

The following statement sets the expiration date for the password of  super user to December 31 2020:

1
2
ALTER ROLE super
VALID UNTIL 'December 31, 2020';

To verify the result, you can view the detailed information of user:

1
2
3
4
5
6
postgres=# \du super;
                            List of roles
Role name |                 Attributes                  | Member of
-----------+---------------------------------------------+-----------
super     | Superuser, Cannot login                    +| {}
           | Password valid until 2020-12-31 00:00:00+07 |

Note that using the ALTER ROLE statement will transfer the password to the server in cleartext. In addition, the cleartext password may be logged in the psql’s command history or the server log.

In this tutorial, you have learned how to change the password of a PostgreSQL user using the ALTER ROLE statement.

Previous Tutorial: Deleting Tablespaces Using PostgreSQL DROP TABLESPACE Statement
Next Tutorial: Backing Up Databases Using PostgreSQL Backup Tools

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete
  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • How To Change The Password of a PostgreSQL User
  • PostgreSQL AGE Function
  • PostgreSQL DATE_PART Function
  • PostgreSQL List Users
  • PostgreSQL NOW Function
  • PostgreSQL DATE_TRUNC Function
  • PostgreSQL TO_DATE Function: Convert String to Date
  • A Look at PostgreSQL User-defined Data Types
  • PostgreSQL Copy Database Made Easy
  • How to Get Table, Database, Indexes, Tablespace, and Value Size in PostgreSQL

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.