Postgresql Docker角色不存在 [英] Postgresql Docker role does not exist

查看:142
本文介绍了Postgresql Docker角色不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

$ 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;  

我的计划是在Django的docker上使用此数据库,因此首先要检查我是否可以连接,但是我不能。

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

$ 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

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

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

为什么会出现此错误?该角色存在,或者至少对我来说似乎......

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

推荐答案

问题很简单,我的计算机已经存在运行我不知道的Postgres实例仍在:5432 上运行(不在Docker内部),并通过以下方式检查:

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:

$ lsof -n -i:5432 | grep LISTEN

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

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

$ pg-stop

然后我没问题连接到Docker

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

该问题最近通过了10,000次查看,因此我认为我应该详细说明为什么会发生这种情况。

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

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

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:

Error: pg_config executable not found.

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

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

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

现在,在Mac上,您需要对brew做同样的事情:

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

brew install postgresql

一件事我没有意识到的是,在Mac上,执行上述操作不仅会安装所需的库,而且还要:5432 上启动数据库。因为这都是在后台完成的,所以我没有想到这是问题所在,因为没有弹出任何通常的错误来告知端口正在使用中,等等。

One thing that I didn't realise, is that on Mac, doing the above will not only install required libraries, but also start 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...

这篇关于Postgresql Docker角色不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆