【问题标题】:Postgres Error: Role <username> doesn't exist, role <username> already existsPostgres 错误:角色 <用户名> 不存在,角色 <用户名> 已存在
【发布时间】:2017-09-02 06:22:04
【问题描述】:

我在尝试连接到我的 postgres 数据库时遇到了严重问题。我运行'psql'然后我得到这个错误“psql:FATAL:角色“”不存在”。然后我使用 psql -U postgres 登录到 postgres,尝试用我的用户名创建一个角色,然后我得到这个错误“角色已经存在”。我到处寻找解决方案。有谁知道发生了什么?我在 Windows 10 上。

【问题讨论】:

  • 可能是大小写不同? psql 参数区分大小写,但 CREATE ROLE 语句中的名称折叠为小写(除非你用双引号括起来)。
  • 您能否引用您尝试过的确切的psql 命令行和您运行的确切的CREATE ROLE 语句?
  • 我也有同样的问题。 sudo -u postgres createuser jonny 返回createuser: creation of new role failed: ERROR: role "jonny" already existspsql 返回psql: FATAL: role "jonny" does not exist 这是薛定谔的角色。 Postgres 是一个垃圾系统,它甚至不知道角色是否存在。

标签: postgresql


【解决方案1】:

您可能安装了两个版本的 postgres。至少,在 Ubuntu 上,这就是我遇到的问题。我不得不(警告这可能会删除数据库,我不知道: sudo apt remove postgres\*)这将选择所有版本的 Postgres 并卸载它们。

在 Windows 上,使用图形界面卸载后,您将验证没有其他 postgres 服务器正在运行。

然后重新安装正确版本的 Postgres(对我来说,Odoo 11 是 9.4)

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-9.4

然后,我做了:(sudo/su 仅适用于 POSIX 系统)

sudo su - postgres -c "createuser -s $USER"
sudo -u postgres psql -c "ALTER USER $USER WITH SUPERUSER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO $USER;"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO $USER;"
sudo -u postgres psql -c "CREATE DATABASE $USER;"
psql

如果你想要一个备用用户

sudo su - postgres -c "createuser -s backup"
sudo -u postgres psql -c "GRANT SELECT ON ALL TABLES IN SCHEMA public TO backup;"
sudo -u postgres psql -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT SELECT ON TABLES TO backup;"

您也可以尝试使用 pgadmin3,但我发现该界面很难

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-01
    • 2012-08-08
    • 1970-01-01
    • 2018-07-14
    • 2013-03-13
    • 2011-11-04
    • 2015-08-16
    • 1970-01-01
    相关资源
    最近更新 更多