Reputation: 83
How can we create multiple databases in PostgreSQL with different users and passwords?
for example - I created an AWS PostgreSQL instance and now I want to create 3 databases each having separate access like
Database01
User - user01
Password - password01
Database02
User - user02
Password - password02
Database03
User - user03
Password - password03
Each database should only have full access to their user and should be restricted to create and delete anything from the other databases and should only have the read permissions.
As I am absolutely new to PostgreSQL and would like to achieve the above scenario manually as well as with automation.
Any help is appreciated.
Upvotes: 2
Views: 4910
Reputation: 83
These are the steps I followed:
Connect to the AWS RDS instance using PgAdmin4 GUI using Postgres ADMIN user.
Create 3 databases [database01, 02 and 03] with admin user.
Create users with an admin user with the following command:
CREATE USER user01 with ENCRYPTED PASSWORD 'password01';
CREATE USER user02 with ENCRYPTED PASSWORD 'password02';
CREATE USER user03 with ENCRYPTED PASSWORD 'password03';
At the SQL level, every user can indeed connect to a newly created database, until the following SQL commands are used:
REVOKE connect ON DATABASE database01 FROM PUBLIC;
REVOKE connect ON DATABASE database02 FROM PUBLIC;
REVOKE connect ON DATABASE database03 FROM PUBLIC;
Once done, each user or role that should be able to connect has to be granted explicitly the connect privilege:
GRANT CONNECT ON DATABASE database01 TO user01;
GRANT CONNECT ON DATABASE database02 TO user02;
GRANT CONNECT ON DATABASE database03 TO user03;
Once we have granted the connection, we can also grant full privileges to users using the below command:
GRANT ALL PRIVILEGES ON DATABASE database01 to user01;
GRANT ALL PRIVILEGES ON DATABASE database02 to user02;
GRANT ALL PRIVILEGES ON DATABASE database03 to user03;
Upvotes: 3