简体   繁体   English

使用不同的用户和密码创建多个 PostgreSQL 数据库

[英]Create Multiple PostgreSQL Databases with Different Users and Passwords

How can we create multiple databases in PostgreSQL with different users and passwords?我们如何使用不同的用户和密码在 PostgreSQL 中创建多个数据库?

for example - I created an AWS PostgreSQL instance and now I want to create 3 databases each having separate access like例如 - 我创建了一个 AWS PostgreSQL 实例,现在我想创建 3 个数据库,每个数据库都有单独的访问权限,例如

Database01数据库01

 User - user01     
 Password - password01

Database02数据库02

   User - user02
   Password - password02

Database03数据库03

   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.因为我对 PostgreSQL 完全陌生,并且希望手动和自动化实现上述场景。

Any help is appreciated.任何帮助表示赞赏。

These are the steps I followed:这些是我遵循的步骤:

  1. Connect to the AWS RDS instance using PgAdmin4 GUI using Postgres ADMIN user.使用 Postgres ADMIN 用户使用 PgAdmin4 GUI 连接到 AWS RDS 实例。

  2. Create 3 databases [database01, 02 and 03] with admin user.使用 admin 用户创建 3 个数据库 [database01、02 和 03]。

  3. 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';
  1. At the SQL level, every user can indeed connect to a newly created database, until the following SQL commands are used:在 SQL 级别,每个用户确实可以连接到新创建的数据库,直到使用以下 SQL 命令:

     REVOKE connect ON DATABASE database01 FROM PUBLIC; REVOKE connect ON DATABASE database02 FROM PUBLIC; REVOKE connect ON DATABASE database03 FROM PUBLIC;
  2. 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;
  3. 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM