postgresql psql:致命:角色“postgres”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15301826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:04:46  来源:igfitidea点击:

psql: FATAL: role "postgres" does not exist

macospostgresqlterminal

提问by user805981

I'm a postgres novice.

我是 postgres 新手。

I installed the postgres.app for mac. I was playing around with the psql commands and I accidentally dropped the postgres database. I don't know what was in it.

我为 mac 安装了 postgres.app。我在玩 psql 命令时不小心删除了 postgres 数据库。我不知道里面有什么。

I'm currently working on a tutorial: http://www.rosslaird.com/blog/building-a-project-with-mezzanine/

我目前正在编写一个教程:http: //www.rosslaird.com/blog/building-a-project-with-mezzanine/

And I'm stuck at sudo -u postgres psql postgres

我被困在 sudo -u postgres psql postgres

ERROR MESSAGE: psql: FATAL: role "postgres" does not exist

错误信息: psql: FATAL: role "postgres" does not exist

$ which psql

$ 哪个 psql

/Applications/Postgres.app/Contents/MacOS/bin/psql

This is what prints out of psql -l

这是打印出来的 psql -l

                                List of databases
    Name    |   Owner    | Encoding | Collate | Ctype |     Access privileges     
------------+------------+----------+---------+-------+---------------------------
 user       | user       | UTF8     | en_US   | en_US | 
 template0  | user       | UTF8     | en_US   | en_US | =c/user                  +
            |            |          |         |       | user      =CTc/user      
 template1  | user       | UTF8     | en_US   | en_US | =c/user                  +
            |            |          |         |       | user      =CTc/user      
(3 rows)

So what are the steps I should take? Delete an everything related to psql and reinstall everything?

那么我应该采取哪些步骤呢?删除与 psql 相关的所有内容并重新安装所有内容?

Thanks for the help guys!

感谢您的帮助!

回答by A.H.

NOTE: If you installed postgres using homebrew, see the comment from @user3402754 below.

注意:如果您使用自制软件安装了 postgres,请参阅下面来自 @user3402754 的评论。

Note that the error message does NOTtalk about a missing database, it talks about a missing role. Later in the login process it might also stumble over the missing database.

请注意,错误消息不是关于缺少数据库,而是关于缺少角色。在登录过程的后期,它也可能会偶然发现丢失的数据库。

But the first step is to check the missing role: What is the output within psqlof the command \du? On my Ubuntu system the relevant line looks like this:

但第一步是检查缺少的角色:psql命令中的输出是什么\du?在我的 Ubuntu 系统上,相关行如下所示:

                              List of roles
 Role name |            Attributes             | Member of 
-----------+-----------------------------------+-----------
 postgres  | Superuser, Create role, Create DB | {}        

If there is not at least one role with superuser, then you have a problem :-)

如果没有至少一个角色superuser,那么你就有问题了:-)

If there is one, you can use that to login. And looking at the output of your \lcommand: The permissions for useron the template0and template1databases are the same as on my Ubuntu system for the superuser postgres. So I think your setup simple uses useras the superuser. So you could try this command to login:

如果有,您可以使用它登录。而且看你的输出\l命令:对于许可usertemplate0template1数据库是一样的我的Ubuntu系统的超级用户postgres。所以我认为您的设置简单地user用作超级用户。所以你可以试试这个命令来登录:

sudo -u user psql user

If useris really the DB superuser you can create another DB superuser and a private, empty database for him:

如果user真的是数据库超级用户,您可以为他创建另一个数据库超级用户和一个私有的空数据库:

CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;

But since your postgres.app setup does not seem to do this, you also should not. Simple adapt the tutorial.

但是由于您的 postgres.app 设置似乎没有这样做,您也不应该这样做。简单适应教程。

回答by jwd630

The key is "I installed the postgres.app for mac." This applicationsets up the local PostgreSQL installation with a database superuser whose role name is the same as your login (short) name.

关键是“我为 Mac 安装了 postgres.app”。此应用程序使用数据库超级用户设置本地 PostgreSQL 安装,其角色名称与您的登录(短)名称相同。

When Postgres.app first starts up, it creates the $USER database, which is the default database for psql when none is specified. The default user is $USER, with no password.

当 Postgres.app 首次启动时,它会创建 $USER 数据库,当没有指定时,它是 psql 的默认数据库。默认用户是 $USER,没有密码。

Some scripts (e.g., a database backup created with pgdumpon a Linux systsem) and tutorials will assume the superuser has the traditional role name of postgres.

某些脚本(例如,pgdump在 Linux 系统上创建的数据库备份)和教程将假定超级用户具有传统角色名称postgres.

You can make your local install look a bit more traditional and avoid these problems by doing a one time:

您可以让您的本地安装看起来更传统一些,并通过执行一次来避免这些问题:

/Applications/Postgres.app/Contents/Versions/9.*/bin/createuser -s postgres

which will make those FATAL: role "postgres" does not existgo away.

这将使那些FATAL: 角色“postgres”不存在消失。

回答by Nitin

For MAC:

对于 MAC:

  1. Install Homebrew
  2. brew install postgres
  3. initdb /usr/local/var/postgres
  4. /usr/local/Cellar/postgresql/<version>/bin/createuser -s postgresor /usr/local/opt/postgres/bin/createuser -s postgreswhich will just use the latest version.
  5. start postgres server manually: pg_ctl -D /usr/local/var/postgres start
  1. 安装 Homebrew
  2. brew install postgres
  3. initdb /usr/local/var/postgres
  4. /usr/local/Cellar/postgresql/<version>/bin/createuser -s postgres或者/usr/local/opt/postgres/bin/createuser -s postgres只使用最新版本。
  5. 手动启动 postgres 服务器: pg_ctl -D /usr/local/var/postgres start


To start server at startup

在启动时启动服务器

  • mkdir -p ~/Library/LaunchAgents
  • ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  • launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
  • mkdir -p ~/Library/LaunchAgents
  • ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  • launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist


Now, it is set up, login using psql -U postgres -h localhostor use PgAdmin for GUI.

现在,它被设置、登录psql -U postgres -h localhost或使用 PgAdmin for GUI。

By default user postgreswill not have any login password.

默认情况下,用户postgres将没有任何登录密码。

Check this site for more articles like this: https://medium.com/@Nithanaroy/installing-postgres-on-mac-18f017c5d3f7

查看此站点以获取更多此类文章:https: //medium.com/@Nithanaroy/installing-postgres-on-mac-18f017c5d3f7

回答by SubstanceMX

createuser postgres --interactive

or make a superuser postgresl just with

或者只用一个超级用户 postgresl

createuser postgres -s

回答by isapir

This happens when you run initdbwith a user whose ID is not postgres, without specifying the postgresusername with --username=postgresor -U postgres.

当您initdb使用 ID 不是 的用户运行时会发生这种情况postgres,而未postgres使用--username=postgres或指定用户名-U postgres

The database cluster is then created with the system's user account that you used to run initdb, and it is given superuser permissions.

然后使用您用来运行 initdb 的系统用户帐户创建数据库集群,并授予它超级用户权限。

To fix it, simply create a new user named postgreswith the option --superuserusing the createuserutility that comes with Postgres. The utility can be found in the Postgres' bindirectory. e.g.

要修复它,只需使用Postgres 附带的实用程序创建一个postgres以该选项命名的新用户。该实用程序可以在 Postgres 的目录中找到。例如--superusercreateuserbin

createuser --superuser postgres

If you have a custom hostname or port then be sure to set the appropriate options.

如果您有自定义主机名或端口,请确保设置适当的选项

Don't forget to delete the other user account that was created for you by initdb.

不要忘记删除由 initdb 为您创建的其他用户帐户。

回答by Kevin Zhao

For me, this code worked:

对我来说,这段代码有效:

/Applications/Postgres.app/Contents/Versions/9.4/bin/createuser -s postgres

it came from here: http://talk.growstuff.org/t/fatal-role-postgres-does-not-exist/216/4

它来自这里:http: //talk.growstuff.org/t/fatal-role-postgres-does-not-exist/216/4

回答by Beau Barker

I needed to unset $PGUSER:

我需要取消设置$PGUSER

$ unset PGUSER
$ createuser -s postgres

回答by ruzenhack

Firstyou need create a user:

首先你需要创建一个用户:

sudo -u postgres createuser --superuser $USER

Afteryou create a database:

您创建一个数据库:

sudo -u postgres createdb $USER

Change$USERto your system username.

更改$USER为您的系统用户名。

You can see the the complete solution here.

您可以在此处查看完整的解决方案。

回答by Alex Levine

Running this on the command line should fix it

在命令行上运行它应该可以修复它

/Applications/Postgres.app/Contents/Versions/9.4/bin/createdb <Mac OSX Username Here>

/Applications/Postgres.app/Contents/Versions/9.4/bin/createdb <Mac OSX Username Here>

回答by Daniel Vérité

Dropping the postgresdatabase doesn't really matter. This database is initially empty and its purpose is simply for the postgresuser to have a kind of "home" to connect to, should it need one.

删除postgres数据库并不重要。这个数据库最初是空的,它的目的只是让postgres用户有一种“家”可以连接,如果需要的话。

Still you may recreate it with the SQL command CREATE DATABASE postgres;

您仍然可以使用 SQL 命令重新创建它 CREATE DATABASE postgres;

Note that the tutorial mentioned in the question is not written with postgres.appin mind. Contrary to PostgreSQL for Unix in general, postgres.apptries to look like a normal application as opposed to a service that would be run by a dedicated postgresuser having different privileges than your normal user. postgres.appis run and managed by your own account.

请注意,问题中提到的教程并不是在编写时postgres.app考虑到的。与 Unix 的 PostgreSQL 通常相反,它postgres.app试图看起来像一个普通的应用程序,而不是一个由postgres与普通用户具有不同权限的专用用户运行的服务。postgres.app由您自己的帐户运行和管理。

So instead of this command: sudo -u postgres psql -U postgres, it would be more in the spirit of postgres.app to just issue: psql, which automatically connects to a database matching your users's name, and with a db account of the same name that happens to be superuser, so it can do anything permissions-wise.

所以代替这个命令: sudo -u postgres psql -U postgres,它会更符合 postgres.app 的精神,只是发出: psql,它会自动连接到与您的用户名匹配的数据库,并使用一个同名的数据库帐户,恰好是超级用户,所以它可以在权限方面做任何事情。