How to create a user and grant permissions in PostgreSQL?
To create a user and grant permissions in PostgreSQL, you can follow these steps:
- Connect to the PostgreSQL database as a superuser.
sudo -u postgres psql
- Create a new user:
CREATE ROLE new_user WITH LOGIN PASSWORD 'password';
- Grant database access permissions to new users.
GRANT CONNECT ON DATABASE database_name TO new_user;
- Grant all permissions of specific databases to new users.
GRANT ALL PRIVILEGES ON DATABASE database_name TO new_user;
- Grant new users permission to SELECT, INSERT, UPDATE, and DELETE on specific table.
GRANT SELECT, INSERT, UPDATE, DELETE ON table_name TO new_user;
- If necessary, new users can be granted additional specific permissions, such as creating databases, creating tables, and so on.
ALTER ROLE new_user CREATEDB;
- 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.