我在尝试连接我的postgres数据库时遇到了严重的问题。我运行'psql',然后出现错误提示"psql: FATAL: role "" does not exist"。然后我使用psql -U postgres登录到postgres,尝试使用我的用户名创建一个角色,然后我得到了这个错误提示"role already exists"。我已经在各处搜索了解决方案。有人知道发生了什么吗?我在使用Windows 10。
我在尝试连接我的postgres数据库时遇到了严重的问题。我运行'psql',然后出现错误提示"psql: FATAL: role "" does not exist"。然后我使用psql -U postgres登录到postgres,尝试使用我的用户名创建一个角色,然后我得到了这个错误提示"role already exists"。我已经在各处搜索了解决方案。有人知道发生了什么吗?我在使用Windows 10。
你可能安装了两个版本的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,但我发现那个界面很难用
psql
参数是区分大小写的,但在CREATE ROLE
语句中的名称会被折叠为小写(除非您将其用双引号括起来)。 - Nick Barnespsql
命令行和你运行的确切的CREATE ROLE
语句吗? - Laurenz Albesudo -u postgres createuser jonny
返回createuser: creation of new role failed: ERROR: role "jonny" already exists
,但是psql
返回psql: FATAL: role "jonny" does not exist
。这是薛定谔的角色。Postgres 是一个垃圾系统,它甚至不知道角色是否存在。 - Jonathan