在PostgreSQL中创建一个超级用户

62
我正在寻找使用Vagrant设置Rails环境,为此目的,该虚拟机已通过bash shell方法进行配置,并包含以下行:
sudo -u postgres createuser -s with password ''
但是我遇到了配置错误:
createuser: too many command-line arguments (first is "with")
你能帮我找到正确的语法来创建一个带有密码的超级用户吗?谢谢。

2
错误可能是有用的。 - Alan B
我的错,完成了。你认为我这样做是正确的吗? - Joseph Palmezano
5个回答

79

使用单个语句在 psql 中执行:

CREATE ROLE username WITH LOGIN SUPERUSER PASSWORD 'password';

例如:

CREATE ROLE dummy WITH LOGIN SUPERUSER PASSWORD '123456';


5
你需要在密码周围使用单引号,双引号不起作用。 - Isaac Pak
似乎不再起作用了:在“用户名”附近有语法错误。 - noone392
@noone392,我刚刚用 pg 14.1 进行了测试,它仍然可以工作。 - Eric
@Eric 嗯,如果我在Postgres 12中在名称周围放置单引号,似乎可以工作,不确定发生了什么。 - noone392
@noone392,你的用户名是什么? - Eric

49

解决方法:

sudo -u postgres createuser -s -i -d -r -l -w <<username>>
sudo -u postgres psql -c "ALTER ROLE <<username>> WITH PASSWORD '<<password>>';"

我知道这不是一个优雅的解决方案,但现在它可以做到


38

适用于 PostgreSQL 8.1 及更新版本

创建超级用户:

CREATE USER username SUPERUSER;

如果您需要指定密码:

CREATE USER username WITH SUPERUSER PASSWORD 'passwordstring';

6
在 PG14 中,我必须省略掉 'WITH',因此 CREATE USER username SUPERUSER PASSWORD 'passwordstring'; - erwaman
1
@erwaman可以使用,但必须放在前面。例如:CREATE USER admin WITH SUPERUSER PASSWORD 'passwordstring'; - Andre Pena
1
感谢您的澄清,@AndréPena! - erwaman

17

创建一个PostgreSQL用户,按照以下步骤进行: 在命令行中,以服务器的根用户身份键入以下命令:

su - postgres

现在你可以作为PostgreSQL超级用户运行命令。要创建一个用户,请输入以下命令:

createuser --interactive --pwprompt

At the Enter name of role to add: prompt, type the user's name.
At the Enter password for new role: prompt, type a password for the user.
At the Enter it again: prompt, retype the password.
At the Shall the new role be a superuser? prompt, type y if you want to grant superuser access. Otherwise, type n.
At the Shall the new role be allowed to create databases? prompt, type y if you want to allow the user to create new databases. Otherwise, type n.
At the Shall the new role be allowed to create more new roles? prompt, type y if you want to allow the user to create new users. Otherwise, type n.

PostgreSQL 会按照您指定的设置创建用户。


1
问题是我想通过一个按顺序执行的脚本创建一个带密码的超级用户。`sudo -u postgres createuser postgres with password 'postgres' -s

设置超级不安全的默认值(仅限开发)

sudo sh -c "echo 'local all postgres peer\nlocal all all peer\nhost all all 127.0.0.1/32 md5\nhost all all ::1/128 md5' > /etc/postgresql/10/machine/pg_hba.conf" sudo /etc/init.d/postgresql reload`我是个新手,你能解释一下你的答案吗?我的操作方式正确吗?
- Joseph Palmezano

0
例如,根据CREATE ROLECREATE GROUPCREATE USER,您可以按照下面的示例创建超级用户john,密码为apple。*您必须使用任何超级用户(例如postgres)登录,并且可以省略可选的WITH参数。
CREATE ROLE john WITH LOGIN SUPERUSER PASSWORD 'apple';

或者:

CREATE GROUP john WITH LOGIN SUPERUSER PASSWORD 'apple';

或者:

CREATE USER john WITH LOGIN SUPERUSER PASSWORD 'apple';
  • 默认情况下,没有 LOGINCREATE USER 仍然具有登录权限。
  • 默认情况下,PostgreSQL 需要密码,所以你应该设置 PASSWORD
  • 你可以省略可选的 WITH
  • PASSWORD 中,你必须使用 '' 而不是 "",否则会出错。

此外,如果你想删除超级用户 john,首先运行以下 SQL。*我的答案解释了如何正确删除用户(角色):

REASSIGN OWNED BY john TO postgres;
DROP OWNED BY john;

最后,你可以使用以下 SQL 语句删除超级用户 john
DROP ROLE john;

或者:

DROP GROUP john;

或者:

DROP USER john;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接