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

谢谢你们的帮助!


当前回答

如果你从Brew安装了postgres,并且使用的是Apple Silicon (M1) mac,在你的终端上运行这个:

/opt/homebrew/opt/postgresql/bin/createuser -s postgres

如果你使用的是Intel (x86) mac,在你的终端上运行这个:

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

其他回答

为了它的价值,我有ubuntu和许多软件包安装,它去与它冲突。

对我来说,正确答案是:

sudo -i -u postgres-xc
psql

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

Brew服务启动postgresql

then

echo $ USER

您将看到您的postgres用户名。

通过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

MAC:

安装自酿酒 酿造安装postgres initdb /usr/local/var/postgres /usr/local/ cellar /postgresql/<version>/bin/createuser -s postgres或/usr/local/opt/ postgress /bin/createuser -s postgres只使用最新版本。 手动启动postgres服务器:pg_ctl -D /usr/local/var/postgres start


在启动时启动服务器

mkdir -p ~/库/启动代理 Ln -sfv /usr/local/opt/postgresql/*。plist ~ /图书馆/ LaunchAgents launchctl load ~/Library/LaunchAgents/homebrew.mx .postgresql.plist


现在,它已经设置好了,使用psql -U postgres -h localhost或使用PgAdmin进行GUI登录。

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

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

上下文

我添加了一个我在这里没有看到的情况的答案,这是一个边缘情况,如果您在同一台机器上有多个用户,并且试图使用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]