'Permission denied' when deleting the 'cli_login_postgres' role
Last edited: 10/14/2026
什么是 CLI 登录角色?#
🌐 What are CLI login roles?
在 Supabase 生态系统中,会生成特定的数据库角色,以帮助你的本地开发环境与远程数据库之间的通信。了解这些角色对于管理数据库的安全状态非常关键。
🌐 In the Supabase ecosystem, specific database roles are generated to help communication between your local development environment and your remote database. Understanding these roles is key to managing your database's security posture.
关键技术术语:
cli_login_postgres: 一个由 Supabase CLI 自动创建的临时角色,用于在没有明确提供数据库密码的情况下,执行迁移或查询等管理任务。supabase_admin: 一个管理核心数据库基础设施的内部系统级角色。它“拥有”某些系统生成的角色,这意味着普通用户无法修改或删除它们。rolvaliduntil: 一个 PostgreSQL 属性,用于设置角色密码的过期时间戳。在这种情况下,这些角色通常在创建后几百秒内就会过期。NOLOGIN: 一个角色属性,它可以防止某个角色被用来建立新的数据库连接,有效地禁用该角色,同时保持其在系统目录中的记录。
理解问题:“权限被拒绝”#
🌐 Understanding the problem: "Permission denied"
如果你尝试通过 SQL 编辑器或普通的数据库连接执行 DROP ROLE cli_login_postgres,你会遇到“权限被拒绝”的错误。即使你以 postgres 用户身份登录,也没有管理权限去移除这个特定的角色,因为它是由内部的 supabase_admin 系统管理的。
🌐 If you attempt to execute DROP ROLE cli_login_postgres via the SQL Editor or a standard database connection, you will encounter a "permission denied" error. Even when logged in as the postgres user, you lack the administrative authority to remove this specific role because it is managed by the internal supabase_admin system.
角色持续存在的原因:
这个角色是托管生命周期的一部分。虽然你可以通过将角色设置为 NOLOGIN 来降低风险,但记录仍然会保留在 pg_roles 表中。此外,因为 Supabase CLI 依赖这个角色进行无密码认证,所以下次在没有密码的情况下运行 CLI 命令时,它会自动重新创建这个角色。
如何解决:永久移除角色#
🌐 How to resolve: Permanently removing the role
标准 SQL 命令不足以管理由平台控制的角色。要完全从目录中移除该角色,你必须使用 Supabase 管理 API。
🌐 Standard SQL commands are insufficient for roles administered by the platform. To remove the role from the catalog entirely, you must use the Supabase Management API.
-
生成访问令牌: 从你的 Supabase 仪表板账户设置中获取个人访问令牌。
-
执行 API 删除请求: 使用
DELETE请求访问 CLI 登录角色端点。这会通知管理系统使用其提升的系统权限删除cli_login_postgres角色(及其只读对应角色)。curl -X DELETE "https://api.supabase.com/v1/projects/your_project/cli/login-role" \-H "Authorization: Bearer <your-personal-access-token>" -
验证删除: 在你的 SQL 编辑器中运行以下查询,以确认角色已被清除:
select rolname from pg_roles where rolname = 'cli_login_postgres';
预防:避免角色重建#
🌐 Prevention: Avoiding role recreation
为了确保将来不会重新创建该角色,你必须改变与 Supabase CLI 的交互方式。只有当 CLI 对没有数据库密码的关联项目运行命令时,才会生成该角色。
🌐 To ensure the role is not recreated in the future, you must change how you interact with the Supabase CLI. The role is only generated when the CLI runs a command against a linked project without a database password.
为了防止重复创建,请始终使用以下方法之一提供你的数据库密码:
🌐 To prevent recreation, always provide your database password using one of these methods:
-
环境变量: 在你的本地终端或
.env文件中设置SUPABASE_DB_PASSWORD变量。 -
命令标志: 执行命令时使用密码标志(通常是
-p或--password):supabase db query --linked -p 'your_database_password' "SELECT 1;"
通过提供密码,CLI 会直接以 postgres 用户身份进行认证,而不是临时分配 cli_login_postgres 角色。
🌐 By providing the password, the CLI authenticates directly as the postgres user rather than provisioning a temporary cli_login_postgres role.