在 postgres 中创建超级用户

问题描述 投票:0回答:5

我正在寻找使用 Vagrant 设置 Rails 环境,为此,它是通过 bash shell 方法配置的,其中包括以下行:

sudo -u postgres createuser <superuserusername> -s with password '<superuserpassword>'

但我遇到配置错误,

createuser: too many command-line arguments (first is "with")

您可以帮助我使用正确的语法来创建带密码的超级用户吗?谢谢

postgresql ubuntu vagrant
5个回答
75
投票

psql
内用一条语句完成此操作:

使用登录超级用户密码“密码”创建角色用户名;

例如

使用登录超级用户密码“123456”创建角色虚拟;


46
投票

解决方法:

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

我知道这不是一个优雅的解决方案,但现在就可以了😊


36
投票

对于 PostgreSQL 版本 8.1 及更高版本

创建超级用户:

CREATE USER username SUPERUSER;

如果需要指定密码:

CREATE USER username WITH SUPERUSER PASSWORD 'passwordstring';

16
投票

要创建 PostgreSQL 用户,请按照以下步骤操作: 在命令行中,以服务器的 root 用户身份键入以下命令:

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 使用您指定的设置创建用户。


0
投票

例如,您可以使用下面的这些 SQL 创建超级用户

john
和密码
orange
。 *可以省略
WITH
,这是可选的,
CREATE USER
不带
LOGIN
仍然可以默认拥有登录权限:

CREATE ROLE john WITH LOGIN SUPERUSER PASSWORD 'orange';

或者:

CREATE GROUP username WITH LOGIN SUPERUSER PASSWORD 'orange';

或者:

CREATE USER username WITH LOGIN SUPERUSER PASSWORD 'orange';
© www.soinside.com 2019 - 2024. All rights reserved.