701

在 postgres 中,如何将现有用户更改为超级用户?由于各种原因,我不想删除现有用户。

# alter user myuser ...?
4

8 回答 8

1361
ALTER USER myuser WITH SUPERUSER;

您可以在文档中阅读更多信息

于 2012-05-25T15:46:25.913 回答
69

要扩展上述内容并快速参考:

  • 使用户成为超级用户:ALTER USER username WITH SUPERUSER;
  • 使用户不再是超级用户:ALTER USER username WITH NOSUPERUSER;
  • 只允许用户创建数据库:ALTER USER username CREATEDB;

您还可以使用CREATEROLECREATEUSER允许用户特权而不使他们成为超级用户。

文档

于 2015-09-23T10:36:59.887 回答
30

$ su - postgres
$ psql
$\du;用于查看 db 上
的用户,选择您希望成为超级用户的用户,然后:
$ ALTER USER "user" with superuser;

于 2016-11-07T16:57:28.640 回答
12

有时升级到超级用户可能不是一个好的选择。因此,除了超级用户之外,您还可以使用许多其他选项。打开终端并输入以下内容:

$ sudo su - postgres
[sudo] password for user: (type your password here)
$ psql
postgres@user:~$ psql
psql (10.5 (Ubuntu 10.5-1.pgdg18.04+1))
Type "help" for help.

postgres=# ALTER USER my_user WITH option

还列出了选项列表

SUPERUSER | NOSUPERUSER | CREATEDB | NOCREATEDB  | CREATEROLE | NOCREATEROLE |
CREATEUSER | NOCREATEUSER | INHERIT | NOINHERIT | LOGIN | NOLOGIN | REPLICATION|
NOREPLICATION | BYPASSRLS | NOBYPASSRLS | CONNECTION LIMIT connlimit | 
[ ENCRYPTED | UNENCRYPTED ] PASSWORD 'password' | VALID UNTIL 'timestamp'

所以在命令行中它看起来像

postgres=# ALTER USER my_user WITH  LOGIN

或使用加密密码。

postgres=# ALTER USER my_user  WITH ENCRYPTED PASSWORD '5d41402abc4b2a76b9719d911017c592';

或在特定时间后撤销权限。

postgres=# ALTER USER my_user  WITH VALID UNTIL '2019-12-29 19:09:00';
于 2018-08-27T15:15:08.443 回答
10

运行此命令

alter user myuser with superuser;

如果您想查看用户的权限,请运行以下命令

\du
于 2017-05-08T15:48:49.187 回答
6

您可以创建一个SUPERUSER或提升USER,所以对于您的情况

$ sudo -u postgres psql -c "ALTER USER myuser WITH SUPERUSER;"

或回滚

$ sudo -u postgres psql -c "ALTER USER myuser WITH NOSUPERUSER;"

要防止在设置密码时记录命令,请在其前面插入一个空格,但请检查您的系统是否支持此选项。

$  sudo -u postgres psql -c "CREATE USER my_user WITH PASSWORD 'my_pass';"
$  sudo -u postgres psql -c "CREATE USER my_user WITH SUPERUSER PASSWORD 'my_pass';"
于 2019-04-03T08:14:41.013 回答
4
alter user username superuser;
于 2017-11-12T10:38:45.653 回答
0

如果您因为使用 Amazon Redshift 而达到此目标,则无法分配SUPERUSER

ALTER USER <username> SUPERUSER;

而是分配CREATEUSER

ALTER USER <username> CREATEUSER;

显然,SUPERUSER不是 Amazon Redshift 集群中的可用用户分配。我对此感到非常困惑。

https://docs.aws.amazon.com/redshift/latest/dg/r_superusers.html

显示此的屏幕截图:

于 2021-10-08T17:57:20.483 回答