database Postgresql Docker 角色不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48593016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:07:14  来源:igfitidea点击:

Postgresql Docker role does not exist

databasepostgresqldocker

提问by jupiar

I downloaded the docker container for postgres: https://hub.docker.com/r/library/postgres/, and did the following:

我下载了 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;  

My plan is to use this database on docker with Django, so first want to check I can connect, but I cant.

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

$ 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

I do the same with PSequal.app, says the same thing, Im running on Mac High Sierra.

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

Why am I getting this error? The role exists, or at least it seems it to me...

为什么我收到这个错误?这个角色是存在的,或者至少在我看来是这样的......

回答by jupiar

The problem was simple enough that my computer was already running an instance of Postgres that I was not aware was still running (not inside Docker) on :5432, checked with:

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

$ lsof -n -i:5432 | grep LISTEN

So I remembered I installed it via https://gist.github.com/sgnl/609557ebacd3378f3b72, I ran

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

$ pg-stop

And then I had no problem connecting to the Docker instance.

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

Edit (2019/07/02)

编辑 (2019/07/02)

This question recently passed 10,000 views, so I thought I should elaborate more on why this happened.

这个问题最近的浏览量超过了 10,000,所以我想我应该详细说明为什么会发生这种情况。

Usually running through docker, using python and connecting to a postgres database requires you to install psycopg2, via pip3 install psycopg2, but if you run this command you will get:

通常通过 docker 运行,使用 python 并连接到 postgres 数据库需要您安装psycopg2, via pip3 install psycopg2,但如果您运行此命令,您将获得:

Error: pg_config executable not found.

This is because psycopg2 requires an operating system install of the postgres libraries:

这是因为 psycopg2 需要 postgres 库的操作系统安装:

yum install postgresql-devel
apt-get install postgresql-client

Now, on a Mac, you will need to do the same with brew:

现在,在 Mac 上,您需要对 brew 执行相同的操作:

brew install postgresql

One thing that I didn't realise, is that on Mac, doing the above will not only install required libraries, but alsostart a database on :5432. Because this was all done in the background, it didn't occur to me that this was the problem as none of the usual errors popped up to inform that the port was being used, etc...

我没有意识到的一件事是,在 Mac 上,执行上述操作不仅会安装所需的库,还会:5432. 因为这一切都是在后台完成的,所以我没有想到这是问题所在,因为没有弹出通常的错误来通知端口正在被使用,等等......

回答by MaxiReglisse

In my case, the problem may be due also to the use of a super user who is not postgres. Lhe developers chose the user georchestra in the geOrchestra docker composition. Extract from the dockerfile:

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

environment:
  - POSTGRES_USER=georchestra

HEALTHCHECK --interval=30s --timeout=30s \
  CMD pg_isready -U $POSTGRES_USER

consequently, you will have to use the option "-U georchestra" to connect to the container.

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

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

and of course, trying to connect with the user postgres causes an error:

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

docker exec -it docker_database_1 psql -U postgres
psql: FATAL:  role "postgres" does not exist