How to create a user and grant permissions in PostgreSQL?

To create a user and grant permissions in PostgreSQL, you can follow these steps:

  1. Connect to the PostgreSQL database as a superuser.
sudo -u postgres psql
  1. Create a new user:
CREATE ROLE new_user WITH LOGIN PASSWORD 'password';
  1. Grant database access permissions to new users.
GRANT CONNECT ON DATABASE database_name TO new_user;
  1. Grant all permissions of specific databases to new users.
GRANT ALL PRIVILEGES ON DATABASE database_name TO new_user;
  1. Grant new users permission to SELECT, INSERT, UPDATE, and DELETE on specific table.
GRANT SELECT, INSERT, UPDATE, DELETE ON table_name TO new_user;
  1. If necessary, new users can be granted additional specific permissions, such as creating databases, creating tables, and so on.
ALTER ROLE new_user CREATEDB;
  1. Exit from PostgreSQL:
\q

New users have been created and granted permissions now. You can use the new user to log in to the PostgreSQL database and perform the necessary operations.

广告
Closing in 10 seconds
bannerAds