当前位置: 首页 > 工具软件 > Postgres.app > 使用案例 >

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

壤驷麒
2023-12-01

本文翻译自:psql: FATAL: role “postgres” does not exist

I'm a postgres novice. 我是postgres新手。

I installed the postgres.app for mac. 我安装了postgres.app for mac。 I was playing around with the psql commands and I accidentally dropped the postgres database. 我正在玩psql命令,我不小心删掉了postgres数据库。 I don't know what was in it. 我不知道里面是什么。

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 $ ps 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! 谢谢你的帮助!


#1楼

参考:https://stackoom.com/question/12Che/psql-致命-角色-postgres-不存在


#2楼

Dropping the postgres database doesn't really matter. 删除postgres数据库并不重要。 This database is initially empty and its purpose is simply for the postgres user to have a kind of "home" to connect to, should it need one. 这个数据库最初是空的,它的目的只是让postgres用户有一种“home”来连接,如果需要的话。

Still you may recreate it with the SQL command CREATE DATABASE postgres; 您仍然可以使用SQL命令CREATE DATABASE postgres;重新创建它CREATE DATABASE postgres;

Note that the tutorial mentioned in the question is not written with postgres.app in mind. 请注意,问题中提到的教程不是用postgres.app编写的。 Contrary to PostgreSQL for Unix in general, postgres.app tries to look like a normal application as opposed to a service that would be run by a dedicated postgres user having different privileges than your normal user. 与一般的PostgreSQL for Unix相反, postgres.app试图看起来像普通应用程序,而不是由具有与普通用户不同权限的专用postgres用户运行的服务。 postgres.app is run and managed by your own account. 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 ,它会自动连接到与您的用户名相匹配的数据库,并且具有该数据库的db帐户恰好是超级用户的同名,因此它可以以权限方式执行任何操作。


#3楼

NOTE: If you installed postgres using homebrew, see the comment from @user3402754 below. 注意:如果您使用自制程序安装了postgres,请参阅下面的@ user3402754中的注释。

Note that the error message does NOT talk 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 psql of the command \\du ? 但第一步是检查缺少的角色:命令\\du psql中的输出是什么? On my Ubuntu system the relevant line looks like this: 在我的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 \\l command: The permissions for user on the template0 and template1 databases are the same as on my Ubuntu system for the superuser postgres . 并查看\\l命令的输出: template0template1数据库上的user权限与我的Ubuntu系统上的超级用户postgres So I think your setup simple uses user as the superuser. 所以我认为你的设置简单使用user作为超级用户。 So you could try this command to login: 所以你可以尝试这个命令登录:

sudo -u user psql user

If user is 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. 但是,由于你的postgres.app设置似乎没有这样做,你也不应该这样做。 Simple adapt the tutorial. 简单地适应教程。


#4楼

I don't think that sudo is needed here because psql -l returns a list of databases. 我不认为这里需要sudo,因为psql -l返回数据库列表。 This tells me that initdb was run under the user's current user, not under the postgres user. 这告诉我initdb是在用户的当前用户下运行的,而不是在postgres用户下运行的。

You can just: 你可以:

psql

And continue the tutorial. 继续本教程。

I would suggest AH's general points of creating the postgres user and db because many applications may expect this to exist. 我建议AH创建postgres用户和db的一般要点,因为许多应用程序可能期望它存在。

A brief explanation: 简要说明:

PostgreSQL will not run with administrative access to the operating system. PostgreSQL不会以对操作系统的管理访问权限运行。 Instead it runs with an ordinary user, and in order to support peer authentication (asking the OS who is trying to connect) it creates a user and db with the user that runs the initialization process. 相反,它与普通用户一起运行,并且为了支持对等身份验证(询问尝试连接的操作系统),它会与运行初始化过程的用户一起创建用户和数据库。 In this case it was your normal user. 在这种情况下,它是您的普通用户。


#5楼

The key is "I installed the postgres.app for mac." 关键是“我安装了postgres.app for mac。” This application sets up the local PostgreSQL installation with a database superuser whose role name is the same as your login (short) name. 此应用程序使用数据库超级用户设置本地PostgreSQL安装,其角色名称与登录(短)名称相同。

When Postgres.app first starts up, it creates the $USER database, which is the default database for psql when none is specified. 当Postgres.app首次启动时,它会创建$ USER数据库,当没有指定时,它是psql的默认数据库。 The default user is $USER, with no password. 默认用户是$ USER,没有密码。

Some scripts (eg, a database backup created with pgdump on a Linux systsem) and tutorials will assume the superuser has the traditional role name of postgres . 一些脚本(例如,在Linux pgdump上使用pgdump创建的数据库备份)和教程将假定超级用户具有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 exist go away. 这将使那些致命的:角色“postgres”不存在消失。


#6楼

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

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

 类似资料: