Postgresql Docker角色不存在

jup*_*iar 14 database postgresql docker

我下载了postgres的docker容器:https://hub.docker.com/r/library/postgres/,并执行了以下操作:

$ docker run --name satgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
satgres | "docker-entrypoint.s…" | 5 seconds ago | Up 6 seconds | 0.0.0.0:5432->5432/tcp | satgres
$ docker exec -it c8f1b22cc41e /bin/bash        
# psql -U postgres
psql (10.1)
postgres=# CREATE DATABASE mytest;
postgres=# \password postgres
postgres=# CREATE ROLE ming WITH LOGIN PASSWORD 'pass1234';
postgres=# ALTER ROLE ming CREATEDB;
postgres=# CREATE DATABASE sjk;
postgres=# GRANT ALL PRIVILEGES ON DATABASE youtube TO ming;
postgres=# \connect sjk
youtube=# ALTER ROLE ming lOGIN;  
Run Code Online (Sandbox Code Playgroud)

我的计划是在Django的docker上使用这个数据库,所以首先要检查我可以连接,但我不能.

$ psql -h localhost -p 5432 -U postgres
$ psql: FATAL:  role "postgres" does not exist
$ psql -h localhost -p 5432 -U ming
$ psql: FATAL:  role "ming" does not exist
Run Code Online (Sandbox Code Playgroud)

我对PSequal.app做了同样的事情,同样的说法,我在Mac High Sierra上运行.

为什么我收到此错误?角色存在,或者至少对我来说似乎......

jup*_*iar 30

问题很简单,我的计算机已经运行了一个我不知道仍在运行的Postgres实例(不在Docker中):5432,检查:

$ lsof -n -i:5432 | grep LISTEN
Run Code Online (Sandbox Code Playgroud)

所以我记得我通过https://gist.github.com/sgnl/609557ebacd3378f3b72安装了它,我跑了

$ pg-stop
Run Code Online (Sandbox Code Playgroud)

然后我连接到Docker实例没有问题.

  • 同样的事发生在我身上 你的答案救了我的一天:)谢谢.(对于未来的用户,如果你在mac并使用brew安装postgres然后停止它只需使用`brew services stop postgresql`并且你将能够连接到docker postgres实例) (10认同)
  • 我在我的 MAC 上,所以我不得不这样做:brew services stop postgresql (2认同)
  • 我遇到了同样的问题,并决定在“~/.bash_profile”中创建一个函数来检查端口上运行的内容:“checkaport(){ lsof -n -i:$1 }”。然后你可以在执行 `source ~/.bash_profile` 后像 `checkaport 5432` 一样使用它。 (2认同)

Sac*_*and 17

如果您使用 docker-compose,请postgres在 docker-compose.yml 中添加一个名为的用户,如下所示:

environment:
  - POSTGRES_USER=postgres
    (...rest of variables)
Run Code Online (Sandbox Code Playgroud)

注意:执行以下命令将导致删除所有卷,从而导致数据库中的所有数据丢失

最后,在删除卷的同时重新启动容器:

docker-compose down --volumes && docker-compose up
Run Code Online (Sandbox Code Playgroud)

  • 对于看到它的人,请确保您了解您的所有卷都将被删除。 (3认同)

Max*_*sse 14

在我的情况下,问题也可能是由于使用了不是 postgres 的超级用户。开发人员在 geOrchestra docker 组合中选择了用户 georchestra。从 dockerfile 中提取:

environment:
  - POSTGRES_USER=georchestra

HEALTHCHECK --interval=30s --timeout=30s \
  CMD pg_isready -U $POSTGRES_USER
Run Code Online (Sandbox Code Playgroud)

因此,您必须使用选项“-U georchestra”来连接到容器。

$ docker exec -it docker_database_1 psql -U georchestra
psql (10.5 (Debian 10.5-1.pgdg90+1))
Type "help" for help.
Run Code Online (Sandbox Code Playgroud)

当然,尝试与用户 postgres 连接会导致错误:

docker exec -it docker_database_1 psql -U postgres
psql: FATAL:  role "postgres" does not exist
Run Code Online (Sandbox Code Playgroud)