我是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安装postgresql和brew服务启动postgresql后,我们有一个名为postgres的数据库。默认情况下,我们可以这样打开psql。

psql postgres

然后我们可以像这样在psql控制台中添加任意名称的用户。

CREATE USER postgres

如果我们想要一个超级用户,我们可以在最后加上SUPERUSER。

其他回答

我需要取消$PGUSER:

$ unset PGUSER
$ createuser -s 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

\du命令返回:

角色名称= postgres@implicit_files

命令postgres=# \password postgres返回错误:

错误:角色postgres不存在。

但是postgres=# \password postgres@implicit_files运行正常。

同样,在sudo -u postgres 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]

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

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