在 postgres 中创建一个超级用户

Create a Superuser in postgres

我正在寻找使用 Vagrant 设置 Rails 环境的方法,为此目的,盒子是通过 bash shell 方法提供的,其中包括这一行:

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

但是我遇到配置错误, createuser: too many command-line arguments (first is "with")

你能帮我用正确的语法创建一个带密码的超级用户吗?谢谢

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

su - postgres

您现在可以 运行 命令作为 PostgreSQL superuser.To 创建用户,键入以下命令:

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

解决方法:

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

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

psql:

内的单个语句中完成

CREATE ROLE username WITH LOGIN SUPERUSER PASSWORD 'password';

例如

CREATE ROLE dummy WITH LOGIN SUPERUSER PASSWORD '123456';

对于 PostgreSQL 版本 8.1 和更高版本

创建超级用户:

CREATE USER username SUPERUSER;

如需指定密码:

CREATE USER username WITH SUPERUSER PASSWORD 'passwordstring';