See git gist with instructions here
运行这个:
sudo -u postgres psql
或
psql -U postgres
在你的终端进入 postgres
注意:如果您使用的是 Mac 并且上述两个命令都失败,请跳转到下面关于 Mac 的部分
postgres=#
运行
CREATE USER new_username;
注意:将 new_username 替换为您要创建的用户,在您的情况下是 tom。
postgres=# CREATE USER new_username;
CREATE ROLE
由于您希望该用户能够创建数据库,因此您需要将角色更改为超级用户
postgres=# ALTER USER new_username SUPERUSER CREATEDB;
ALTER ROLE
确认一下,一切都成功了,
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
new_username | Superuser, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
root | Superuser, Create role, Create DB | {}
postgres=#
更新/修改(适用于 Mac):
我最近在我的 Mac 上遇到了类似的错误:
psql: FATAL: role "postgres" does not exist
这是因为我的安装是使用数据库超级用户设置的,其角色名称与您的登录(短)名称相同。
但一些 linux 脚本假定超级用户具有传统的角色名称 postgres
我是如何解决这个问题的?
如果你安装了homebrew 运行:
/usr/local/opt/postgres/bin/createuser -s postgres
如果您使用的是特定版本的 postgres,请说 10.5 然后运行:
/usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres
或:
/usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username
或者:
/usr/local/opt/postgresql@11/bin/createuser -s postgres
如果你安装了postgres.app for Mac 运行:
/Applications/Postgres.app/Contents/Versions/10.5/bin/createuser -s postgres
P.S:将 10.5 替换为您的 PostgreSQL 版本