PostgreSQL User Mangement

Pankaj kushwaha
2 min readMar 20, 2023

--

PostgreSQL is a powerful open-source relational database management system that offers various features for managing users and their permissions. In this article, we will discuss how to manage users in PostgreSQL and the different types of permissions that can be granted.

Managing Users in PostgreSQL PostgreSQL allows you to create, modify, and delete users through SQL commands or using graphical user interfaces such as pgAdmin. The following are the basic commands for managing users in PostgreSQL:

  1. Creating a User To create a new user in PostgreSQL, you can use the CREATE USER command followed by the username and password:
CREATE USER username WITH PASSWORD 'password';

You can also set additional options such as login roles, password expiration, and user privileges:

CREATE USER username WITH
LOGIN
NOSUPERUSER
INHERIT
NOCREATEDB
NOCREATEROLE
PASSWORD 'password'
VALID UNTIL '2023-03-20';
  1. Modifying a User To modify a user’s attributes, you can use the ALTER USER command followed by the username and the attribute to be modified:
sqlCopy code
ALTER USER username WITH PASSWORD 'new_password';

You can also modify other user attributes such as login roles, password expiration, and user privileges:

ALTER USER username WITH
LOGIN
SUPERUSER
CREATEDB
PASSWORD 'new_password'
VALID UNTIL '2024-03-20';
  1. Deleting a User To delete a user in PostgreSQL, you can use the DROP USER command followed by the username:
DROP USER username;

Types of Permissions in PostgreSQL PostgreSQL provides several types of permissions that can be granted to users, including:

  1. SELECT: allows users to retrieve data from tables
  2. INSERT: allows users to add new data to tables
  3. UPDATE: allows users to modify existing data in tables
  4. DELETE: allows users to remove data from tables
  5. EXECUTE: allows users to execute stored procedures and functions
  6. USAGE: allows users to access database objects such as schemas, sequences, and views
  7. CREATE: allows users to create new database objects such as tables and indexes
  8. CONNECT: allows users to connect to a database

Granting Permissions in PostgreSQL To grant permissions to a user in PostgreSQL, you can use the GRANT command followed by the type of permission, the object to which the permission is granted, and the username:

GRANT SELECT ON table_name TO username;

You can also grant multiple permissions at once:

GRANT SELECT, INSERT, UPDATE ON table_name TO username;

Revoking Permissions in PostgreSQL To revoke permissions from a user in PostgreSQL, you can use the REVOKE command followed by the type of permission, the object from which the permission is revoked, and the username:

REVOKE SELECT ON table_name FROM username;

You can also revoke multiple permissions at once:

REVOKE SELECT, INSERT, UPDATE ON table_name FROM username;

Conclusion In summary, PostgreSQL provides a robust user management system that allows you to create, modify, and delete users and grant them various permissions. By understanding these basic commands and permissions, you can effectively manage your PostgreSQL databases and ensure that only authorized users have access to your data.

Thank you for reading this.

--

--

Pankaj kushwaha
Pankaj kushwaha

Written by Pankaj kushwaha

Database/System Administrator | DevOPS | Cloud Specialist | DevOPS

No responses yet