我是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和重新安装一切?

谢谢你们的帮助!


当前回答

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

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

其他回答

关键是“我安装了postgres。mac版App。”此应用程序使用数据库超级用户(角色名与您的登录名(简称)相同)设置本地PostgreSQL安装。

当Postgres。app首先启动,它创建$USER数据库, 当没有指定时,它是PSQL的默认数据库。的 默认用户为$ user,没有密码。

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

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

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

这将使那些FATAL: role "postgres"不存在消失。

当你使用一个ID不是postgres的用户运行initdb时,没有指定postgres的用户名——username=postgres或-U postgres,就会发生这种情况。

然后,使用用于运行initdb的系统用户帐户创建数据库集群,并赋予其超级用户权限。

要解决这个问题,只需使用postgres附带的createuser实用程序创建一个名为postgres的新用户,选项为——superuser。该实用程序可以在Postgres的bin目录中找到。如。

createuser --superuser postgres

如果您有一个自定义主机名或端口,那么一定要设置适当的选项。

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

通过homebrew安装了新的mac (M1)和最新的postgres(14.0),没有什么对我的这个主题有帮助,但我只是重新安装了postgres,它帮助了我:

brew services stop postgresql
rm -rf /opt/homebrew/var/postgres/*
brew reinstall postgresql
initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres
brew services restart postgresql

所以,这是一个奇迹之类的……

然后:

psql -d 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]

在Mac上,执行

createuser -s postgres

在一个终端工作。