我正在设置PostgreSQL 9.1。我不能用PostgreSQL做任何事情:不能createdb,不能createuser;所有操作都返回错误消息

Fatal: role h9uest does not exist

h9uest是我的帐户名,我在这个帐户下执行apt-get安装PostgreSQL 9.1。 根帐户仍然存在类似的错误。


当前回答

sudo su - postgres

psql template1

在PGSQL上创建具有“超级用户”权限的角色

CREATE ROLE username superuser;
eg. CREATE ROLE demo superuser;

然后创建用户

CREATE USER username; 
eg. CREATE USER demo;

为用户分配权限

GRANT ROOT TO username;

然后启用该用户登录,这样你就可以从普通的$ terminal运行:psql template1:

ALTER ROLE username WITH LOGIN;

其他回答

这对我来说很管用:

psql -h localhost -U postgres

Windows用户:psql -U postgres

然后你应该看到PostgreSQL的命令行界面:postgres=#

我用docker-compose做了健康检查。

healthcheck:
  test: ['CMD-SHELL', 'pg_isready']
  interval: 5s
  timeout: 5s
  retries: 5

如果你也有这样的更改用户:

healthcheck:
  test: ['CMD-SHELL', 'pg_isready -U postgres'] # <<<---
  interval: 5s
  timeout: 5s
  retries: 5
psql postgres

postgres=# CREATE ROLE username superuser;
postgres=# ALTER ROLE username WITH LOGIN;
sudo -u postgres createuser --superuser $USER

sudo -u postgres createdb $USER

这绝对适合你。