Postgres 角色
Managing access to your Postgres database and configuring permissions.
Postgres 使用角色的概念来管理数据库访问权限。通常你不会在自己的应用中使用这些角色——它们主要用于配置数据库的系统访问。如果你想配置应用访问权限,那么应该使用行级安全(RLS)。你也可以在 RLS 的基础上实现基于角色的访问控制。
🌐 Postgres manages database access permissions using the concept of roles. Generally you wouldn't use these roles for your own application - they are mostly for configuring system access to your database. If you want to configure application access, then you should use Row Level Security (RLS). You can also implement Role-based Access Control on top of RLS.
用户与角色 #
🌐 Users vs roles
在 Postgres 中,角色可以作为用户或用户组使用。用户是有登录权限的角色,而组(也叫角色组)是没有登录权限的角色,但可以用来管理多个用户的权限。
🌐 In Postgres, roles can function as users or groups of users. Users are roles with login privileges, while groups (also known as role groups) are roles that don't have login privileges but can be used to manage permissions for multiple users.
创建角色 #
🌐 Creating roles
你可以使用 create role 命令创建一个角色:
🌐 You can create a role using the create role command:
1create role "role_name";创建用户 #
🌐 Creating users
在 Postgres 中,角色和用户本质上是一样的,不过如果你想为某个特定角色使用密码登录,那么你可以使用 WITH LOGIN PASSWORD:
🌐 Roles and users are essentially the same in Postgres, however if you want to use password-logins for a specific role, then you can use WITH LOGIN PASSWORD:
1create role "role_name" with login password 'extremely_secure_password';密码 #
🌐 Passwords
你的 Postgres 数据库是你 Supabase 项目的核心,所以确保每个角色始终有一个强大、安全的密码非常重要。以下是一些创建安全密码的建议:
🌐 Your Postgres database is the core of your Supabase project, so it's important that every role has a strong, secure password at all times. Here are some tips for creating a secure password:
- 用密码管理器生成它。
- 设置一个长密码(至少12个字符)。
- 不要使用任何常见的字典词。
- 使用大小写字母、数字和特殊符号。
密码中的特殊符号 #
🌐 Special symbols in passwords
如果你的 Postgres 密码中使用了特殊符号,那么如果你在使用 Postgres 连接字符串时,要记得之后对密码进行 百分比编码,例如,postgresql://postgres.projectref:p%3Dword@aws-0-us-east-1.pooler.supabase.com:6543/postgres
🌐 If you use special symbols in your Postgres password, you must remember to percent-encode your password later if using the Postgres connection string, for example, postgresql://postgres.projectref:p%3Dword@aws-0-us-east-1.pooler.supabase.com:6543/postgres
更改你的项目密码 #
🌐 Changing your project password
当你创建项目时,你也需要输入一个密码。这个密码是你数据库中 postgres 角色的密码。你可以在仪表板的 数据库设置 页面中更新它。除非你完全信任第三方服务,否则 绝对不要 将此密码提供给他们。相反,我们建议你为每个想要授予访问权限的服务创建一个新用户。这也有助于你调试——你可以在 pg_stat_statements 中看到每个角色在数据库中执行的所有查询。
🌐 When you created your project you were also asked to enter a password. This is the password for the postgres role in your database. You can update this from the Dashboard under the Database Settings page. You should never give this to third-party service unless you absolutely trust them. Instead, we recommend that you create a new user for every service that you want to give access too. This will also help you with debugging - you can see every query that each role is executing in your database within pg_stat_statements.
更改密码不会导致任何停机。所有已连接的服务,例如 PostgREST、PgBouncer 以及其他 Supabase 管理的服务,都会自动更新为使用最新密码,以确保可用性。不过,如果你有任何外部服务使用硬编码的用户名/密码连接 Supabase 数据库,就需要手动更新。
🌐 Changing the password does not result in any downtime. All connected services, such as PostgREST, PgBouncer, and other Supabase managed services, are automatically updated to use the latest password to ensure availability. However, if you have any external services connecting to the Supabase database using hardcoded username/password credentials, a manual update will be required.
授予权限 #
🌐 Granting permissions
可以使用 GRANT 命令授予角色对数据库对象的各种权限。权限包括 SELECT、INSERT、UPDATE 和 DELETE。你几乎可以配置对数据库中任何对象的访问权限——包括表、视图、函数和触发器。
🌐 Roles can be granted various permissions on database objects using the GRANT command. Permissions include SELECT, INSERT, UPDATE, and DELETE. You can configure access to almost any object inside your database - including tables, views, functions, and triggers.
撤销权限 #
🌐 Revoking permissions
可以使用 REVOKE 命令撤销权限:
🌐 Permissions can be revoked using the REVOKE command:
1REVOKE permission_type ON object_name FROM role_name;角色层级 #
🌐 Role hierarchy
角色可以按层级组织,一个角色可以继承另一个角色的权限。这简化了权限管理,因为你可以在更高层定义权限,并让它们自动应用到所有子角色。
🌐 Roles can be organized in a hierarchy, where one role can inherit permissions from another. This simplifies permission management, as you can define permissions at a higher level and have them automatically apply to all child roles.
角色继承 #
🌐 Role inheritance
要创建角色层级,首先需要创建父角色和子角色。子角色会继承父角色的权限。可以在创建角色时使用 INHERIT 选项来添加子角色:
🌐 To create a role hierarchy, you first need to create the parent and child roles. The child role will inherit permissions from its parent. Child roles can be added using the INHERIT option when creating the role:
1create role "child_role_name" inherit "parent_role_name";防止继承 #
🌐 Preventing inheritance
在某些情况下,你可能想阻止一个角色拥有子角色关系(通常是超级用户角色)。你可以使用 NOINHERIT 来防止继承关系:
🌐 In some cases, you might want to prevent a role from having a child relationship (typically superuser roles). You can prevent inheritance relations using NOINHERIT:
1alter role "child_role_name" noinherit;Supabase 角色 #
🌐 Supabase roles
Postgres 自带一套 预定义角色。Supabase 在此基础上扩展了一个默认角色集合,当你启动一个新项目时,这些角色会在你的数据库上配置好:
🌐 Postgres comes with a set of predefined roles. Supabase extends this with a default set of roles which are configured on your database when you start a new project:
postgres#
默认的 Postgres 角色。它拥有管理员权限。
🌐 The default Postgres role. This has admin privileges.
anon#
用于未认证的公开访问。当用户未登录时,API(PostgREST)将使用这个角色。
🌐 For unauthenticated, public access. This is the role which the API (PostgREST) will use when a user is not logged in.
authenticator#
API(PostgREST)的一个特殊角色。它的访问权限非常有限,用于验证 JWT,然后“变成”由 JWT 验证决定的另一个角色。
🌐 A special role for the API (PostgREST). It has very limited access, and is used to validate a JWT and then "change into" another role determined by the JWT verification.
authenticated#
对于“认证访问”。这是当用户已登录时,API(PostgREST)将使用的角色。
🌐 For "authenticated access." This is the role which the API (PostgREST) will use when a user is logged in.
service_role#
用于提升访问权限。这个角色由 API(PostgREST)使用,用来绕过行级安全。
🌐 For elevated access. This role is used by the API (PostgREST) to bypass Row Level Security.
supabase_auth_admin#
由 Auth 中间件用于连接数据库并运行迁移。访问范围限定在 auth 模式下。
🌐 Used by the Auth middleware to connect to the database and run migration. Access is scoped to the auth schema.
supabase_storage_admin#
由 Auth 中间件用于连接数据库并运行迁移。访问范围限定在 storage 模式下。
🌐 Used by the Auth middleware to connect to the database and run migration. Access is scoped to the storage schema.
supabase_etl_admin#
supabase_etl_admin 被 Supabase Pipelines 用于 数据库复制。
这个角色:
🌐 This role:
- 将数据库更改复制到目标系统
- 拥有全部读取权限
- 拥有更改数据捕获的复制权限,绕过行级安全
- 可以创建事件触发器
- 可以写入
etl模式
dashboard_user#
用于通过 Supabase UI 运行命令。
🌐 For running commands via the Supabase UI.
supabase_admin#
Supabase 用于管理任务的内部角色,比如执行升级和自动化操作。
🌐 An internal role Supabase uses for administrative tasks, such as running upgrades and automations.
资源 #
🌐 Resources