psql:致命:角色"postgres"不存在

use*_*981 336 postgresql macos terminal

我是postgres新手.

我安装了postgres.app for mac.我正在玩psql命令,我不小心删掉了postgres数据库.我不知道里面是什么.

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

我被困在了 sudo -u postgres psql postgres

错误信息: psql: FATAL: role "postgres" does not exist

$ ps psql

/Applications/Postgres.app/Contents/MacOS/bin/psql
Run Code Online (Sandbox Code Playgroud)

这是打印出来的 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)
Run Code Online (Sandbox Code Playgroud)

那我应该采取什么步骤?删除与psql相关的所有内容并重新安装所有内容?

谢谢你的帮助!

A.H*_*.H. 340

请注意,该错误信息不会谈论丢失的数据库,它谈论缺少的作用.稍后在登录过程中,它可能会偶然发现丢失的数据库.

但第一步是检查缺少的角色:psql命令中的输出是什么\du?在我的Ubuntu系统上,相关的行看起来像这样:

                              List of roles
 Role name |            Attributes             | Member of 
-----------+-----------------------------------+-----------
 postgres  | Superuser, Create role, Create DB | {}        
Run Code Online (Sandbox Code Playgroud)

如果没有至少一个角色superuser,那么你有一个问题:-)

如果有,您可以使用它登录.而且看你的输出\l命令:在权限usertemplate0template1数据库是一样的我的Ubuntu系统的超级用户postgres.所以我认为你的设置简单地user用作超级用户.所以你可以尝试这个命令登录:

sudo -u user psql user
Run Code Online (Sandbox Code Playgroud)

如果user确实是数据库超级用户,您可以为他创建另一个数据库超级用户和一个私有的空数据库:

CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;
Run Code Online (Sandbox Code Playgroud)

但是,由于你的postgres.app设置似乎没有这样做,你也不应该这样做.简单地适应教程.

  • 如果您从`homebrew`安装了Postgres,那么您需要在终端中运行`/ usr/local/opt/postgres/bin/createuser -s postgres` (114认同)
  • 我必须运行 `/usr/local/opt/postgresql/bin/createuser -s postgres` (15认同)
  • 对我来说,用户并没有被创建 - 这有点'使用SUPERUSER PASSWORD'密码'创建用户postgres;`我正在使用postgres v10. (9认同)
  • 如果您使用的是特定版本的 postgres,那么您需要在路径中包含该版本 - `/usr/local/opt/postgresql@11/bin/createuser -s postgres` (7认同)
  • 对于 M1 Mac:`/opt/homebrew/bin/createuser -s postgres` (7认同)
  • 对于 M1+brew+版本: `/opt/homebrew/opt/postgresql@11/bin/createuser -s postgres` (7认同)
  • @user1807782 你救了我的命;我认为你应该将此作为答案。 (2认同)

jwd*_*630 253

关键是"我安装了postgres.app for mac." 此应用程序使用数据库超级用户设置本地PostgreSQL安装,其角色名称与登录(短)名称相同.

当Postgres.app首次启动时,它会创建$ USER数据库,当没有指定时,它是psql的默认数据库.默认用户是$ USER,没有密码.

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

您可以通过一次性使本地安装看起来更传统并避免这些问题:

/Applications/Postgres.app/Contents/Versions/9.*/bin/createuser -s postgres
Run Code Online (Sandbox Code Playgroud)

这将使那些致命的:角色"postgres"不存在消失.

  • 如果您使用的是Homebrew的postgres,那么您需要`/usr/local/Cellar/postgresql/9.2.4/bin/createuser -s postgres`(显然版本为9.2.4). (112认同)
  • 那个`createuser`命令对我有用.谢谢! (10认同)
  • `/Applications/Postgres.app/Contents/Versions/9.3/bin/createuser -s postgres`为我正确运行. (7认同)
  • @RogerLipscombe如果你在安装后运行`brew link postgresql`,就没有必要将整个路径附加到`createuser`,一个简单的`createuser -s postgres`会很好用 (7认同)
  • `createuser -s postgres` 为我工作。我用 Homebrew 安装了 postgres (2认同)

Nit*_*tin 204

对于MAC:

  1. 安装Homebrew
  2. brew install postgres
  3. initdb /usr/local/var/postgres
  4. /usr/local/Cellar/postgresql/<version>/bin/createuser -s postgres或者/usr/local/opt/postgres/bin/createuser -s postgres只使用最新版本.
  5. 手动启动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或使用PgAdmin进行GUI 登录.

默认情况下,用户postgres不会有任何登录密码.

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

  • 使用最新的brew安装,您可以执行`brew services start postgresql`来启动postgres (3认同)

Ami*_*ena 153

如果您在 2024 年在这里:)并且想知道最新的 Postgres 在最新的 macOS (macOS Monterey ) 上可以使用什么

按照这个:

brew install postgresql
createuser -s postgres
brew services restart postgresql
Run Code Online (Sandbox Code Playgroud)

注意:如果您使用的是 docker 化实例,请确保首先设置变量,以便可以连接到该实例:export PGUSER="youruser" export PGHOST="127.0.0.1" export PGPASSWORD="yourpassword。

感谢 @NicolasZ 关于 dockerized 实例的评论。

  • 在蒙特雷运行得很好。 (5认同)
  • 谢谢!,我卸载了postgres,然后我按照你说的做了,它成功了! (2认同)

小智 38

如果您从 brew 安装了 postgres,请在您的终端中运行它:

/usr/local/opt/postgres/bin/createuser -s postgres
Run Code Online (Sandbox Code Playgroud)


小智 32

createuser postgres --interactive
Run Code Online (Sandbox Code Playgroud)

或者只是用超级用户postgresl

createuser postgres -s
Run Code Online (Sandbox Code Playgroud)


Kev*_*hao 16

对我来说,这段代码有效:

/Applications/Postgres.app/Contents/Versions/9.4/bin/createuser -s postgres
Run Code Online (Sandbox Code Playgroud)

它来自这里:http: //talk.growstuff.org/t/fatal-role-postgres-does-not-exist/216/4


isa*_*pir 16

当您initdb使用ID为not的用户运行时postgres,未postgres使用--username=postgres或指定用户名时,就会发生这种情况-U postgres

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

要解决此问题,只需使用Postgres随附的实用程序创建一个postgres带有该选项的新用户即可。该实用程序可以在Postgres的目录中找到。例如--superusercreateuserbin

createuser --superuser postgres
Run Code Online (Sandbox Code Playgroud)

如果您具有自定义的主机名或端口,请确保设置适当的选项

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

  • 这就是我一直在寻找的确切解释!谢谢。 (2认同)

ruz*_*ack 14

首先,您需要创建一个用户:

sudo -u postgres createuser --superuser $USER
Run Code Online (Sandbox Code Playgroud)

您创建一个数据库:

sudo -u postgres createdb $USER
Run Code Online (Sandbox Code Playgroud)

更改 $USER为您的系统用户名.

您可以在此处查看完整的解决方案.


Bea*_*ker 11

我需要取消设置$PGUSER:

$ unset PGUSER
$ createuser -s postgres
Run Code Online (Sandbox Code Playgroud)


use*_*049 9

这篇文章帮助我解决了同样的问题psql: FATAL: role \xe2\x80\x9cpostgres\xe2\x80\x9d does not exist

\n

我使用的是mac,所以我在终端中输入了这个命令:

\n

createuser -s postgres

\n

这对我有用。

\n


kan*_*kyu 9

postgres我们有一个以brew install postgresql和命名的数据库brew services start postgresql。所以我们默认可以这样打开psql。

psql postgres
Run Code Online (Sandbox Code Playgroud)

然后我们可以在 psql 控制台中添加具有任何名称的用户。

CREATE USER postgres
Run Code Online (Sandbox Code Playgroud)

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


Mba*_*pel 9

这对我有用

创建用户-s postgres

注意:我使用的是 mac catalina


Ale*_*ine 8

在命令行上运行它应该修复它

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


Bru*_*JCM 7

如果您正在使用 docker,请确保您没有使用POSTGRES_USER=something_else,因为标准映像使用此变量来了解 PostgreSQL 管理员用户的名称(默认为postgres)。

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


小智 6

如果您从 Brew 安装了 postgres 并且使用的是Apple Silicon (M1) mac,请在您的终端中运行:

/opt/homebrew/opt/postgresql/bin/createuser -s postgres
Run Code Online (Sandbox Code Playgroud)

如果您使用的是Intel (x86) mac,请在终端中运行:

/usr/local/opt/postgres/bin/createuser -s postgres
Run Code Online (Sandbox Code Playgroud)


小智 6

对于 m1 芯片,如果您尚未通过 homebrew 安装 postgresql 软件包,请在终端中安装:

brew install postgre
Run Code Online (Sandbox Code Playgroud)

然后通过以下方式手动创建用户名:

/opt/homebrew/bin/createuser -s <username> 
Run Code Online (Sandbox Code Playgroud)

您的错误可能已修复;但如果你出现错误

致命:数据库“databasename”不存在

那么你必须通过以下方式手动创建数据库:

/opt/homebrew/bin/createdb -U <username>  <databasename>    
Run Code Online (Sandbox Code Playgroud)


Bax*_*Bax 5

这是唯一为我修复它的:

createuser -s -U $USER
Run Code Online (Sandbox Code Playgroud)

  • 这使当前用户成为系统超级用户而不仅仅是 postgres 超级用户。我会说这通常是一个坏主意,因为它绕过了通常的安全策略。您应该使用 `sudo` 来获得临时超级用户权限。 (2认同)