psql: FATAL: 角色"postgres"不存在。
psql: FATAL: 角色"postgres"不存在。
我是一个Postgres的初学者。
我安装了Mac版的postgres.app。我正在玩psql命令,无意中删除了postgres数据库。我不知道里面有什么。
我目前正在学习教程:https://www.rosslaird.com/blog/building-a-project-with-mezzanine/
但我被卡在了sudo -u postgres psql postgres
这一步。
错误信息:psql: FATAL: role \"postgres\" does not exist
$ 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有关的东西,然后重新安装所有内容吗?
谢谢大家的帮助!
Mac用户:
- 安装Homebrew
brew install postgres
initdb /usr/local/var/postgres
/usr/local/Cellar/postgresql/<版本>/bin/createuser -s postgres
或/usr/local/opt/postgres/bin/createuser -s postgres
(使用最新版本)- 手动启动postgres服务器:
pg_ctl -D /usr/local/var/postgres start
开机自动启动
mkdir -p ~/Library/LaunchAgents
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
现在,已经设置好了,可以使用psql -U postgres -h localhost
登录,或使用GUI的PgAdmin。
默认情况下,用户postgres
将没有任何登录密码。
查看此网站以获取更多类似的文章:https://medium.com/@Nithanaroy/installing-postgres-on-mac-18f017c5d3f7
注意:如果您使用homebrew安装了postgres,请查看下面@user3402754的评论。\n\n请注意,错误信息并没有提到缺少数据库,而是缺少角色。在登录过程后,它可能还会遇到缺少数据库的问题。\n\n但是,第一步是检查缺少的角色:在 psql
命令下,输入 \\du
命令会得到什么输出结果?在我的Ubuntu系统中,相关行看起来像这样:\n\n
List of roles Role name | Attributes | Member of -----------+-----------------------------------+----------- postgres | Superuser, Create role, Create DB | {}
\n\n如果没有至少一个拥有 superuser
的角色,那么你就有问题了 :-)\n\n如果有一个,你可以使用它来登录。同时,查看您的 \\l
命令的输出:对于template0
和 template1
数据库上的 user
权限,与我的Ubuntu系统上的超级用户postgres
相同。因此,我认为您的设置只是将user
作为超级用户。因此,您可以尝试使用以下命令进行登录:\n\n
sudo -u user psql user
\n\n如果user
确实是数据库超级用户,则可以创建另一个数据库超级用户以及一个私有的空数据库:\n\n
CREATE USER postgres SUPERUSER; CREATE DATABASE postgres WITH OWNER postgres;
\n\n但是,由于您的postgres.app设置似乎没有这样做,因此您也不应该这样做。只需按照教程进行简单的调整。