我是postgres的新手。

我安装了postgres。我在玩psql命令,我不小心掉了postgres数据库。我不知道里面有什么。

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

我被困在sudo -u postgres psql postgres

错误信息:psql: FATAL: role "postgres"不存在

$ which PSQL

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

这是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)

那么我应该采取哪些步骤呢?删除一切相关的psql和重新安装一切?

谢谢你们的帮助!


当前回答

我今天遇到了类似的问题,实际上我不知道用户名是什么。这里有2件事,如果你在企业和没有系统管理员访问postgres将创建你的企业用户名作为postgres管理用户名。如果你通过Homebrew安装,肯定会发生这种情况。在这种情况下,只需使用brew运行psql服务,并对用户名进行回显

Brew服务启动postgresql

then

echo $ USER

您将看到您的postgres用户名。

其他回答

如果您是MAC (M1)用户,并使用HomeBrew安装了Postgres,那么请遵循以下步骤:

使用哪个psql . exe检查Postgres位置 如果第一个回显为/opt/homebrew/bin/psql,则执行/opt/homebrew/bin/createuser -s postgres

这个想法是使用postgres安装位置创建一个名为“postgres”的用户。因此,您可能需要根据Postgres的位置更改该命令。

如果你从brew安装了postgres,在你的终端上运行:

/usr/local/opt/postgres/bin/createuser -s postgres

上下文

我添加了一个我在这里没有看到的情况的答案,这是一个边缘情况,如果您在同一台机器上有多个用户,并且试图使用postgres服务的用户不是在该机器上安装postgres的用户。

我所尝试过的

在其他类似的命令中,这些命令对我来说都失败了:

createuser -s [your username]
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "[your username]" does not exist
createuser -s postgres
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "[your username]" does not exist
sudo -u postgres createuser --superuser [your username]
# sudo: unknown user: postgres
# sudo: error initializing audit plugin sudoers_audit
psql -U postgres
# psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "postgres" does not exist

原因

原因是postgres角色和[你的用户名](也就是你的命令行中的whoami)都不在postgres中。

解决方案

在这种极端情况下,我必须首先登录安装postgres的用户:

sudo su - [username that installed postgres]

然后为我的新用户创建一个角色:

createuser -s [your username]

如果你正在使用docker,确保你没有使用POSTGRES_USER=something_else,因为这个变量被标准映像用来知道PostgreSQL管理用户的名称(默认为postgres)。

在我的例子中,我使用这个变量的目的是将另一个用户设置到我的特定数据库,但它最终当然改变了主PostgreSQL用户。

如果你在2023年,想知道最新的Postgres在最新的macOS (macOS Monterey)上有什么用

遵循这个:

brew install postgresql
createuser -s postgres
brew services restart postgresql