postgresql 无法连接到 docker 中的 postgres 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30770898/
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
Cannot connect to postgres server in docker
提问by kxxoling
I started a postgresql server in docker and exposed the 5432 port by sudo docker run -it -p 5432:5432 9c421f1a239c bash
and start the postgres server manually inside the docker container, but cannot connect to it with command: psql -h 172.17.0.63 -U venti
. 172.17.0.63
is a right IP, and venti is my pg username. But get error:
我开始在码头工人一个PostgreSQL服务器,并通过公开的5432端口sudo docker run -it -p 5432:5432 9c421f1a239c bash
和手动启动Postgres服务器来泊坞窗容器内,但不能与命令连接到它:psql -h 172.17.0.63 -U venti
。172.17.0.63
是一个正确的IP,而venti 是我的pg 用户名。但得到错误:
psql: could not connect to server: Connection refused
Is the server running on host "172.17.0.63" and accepting
TCP/IP connections on port 5432?
My pg_hba.conf looks like this:
我的 pg_hba.conf 看起来像这样:
local all postgres peer
host all all 0.0.0.0/0 trust
local all all trust
Connecting to pg server inside container works successfully.
连接到容器内的 pg 服务器工作成功。
Dockerfile:
Dockerfile:
FROM ubuntu:12.04
RUN apt-get update
RUN apt-get install -y gcc libc-dev-bin libc6 libc6-dev libssl-dev libkrb5-dev comerr-dev
RUN apt-get install -y postgresql-common libpq-dev postgresql-9.1-postgis --fix-missing
RUN apt-get install -y postgresql postgresql-client
USER postgres
ENV PGDATA /etc/postgresql/9.1/main
ENV LOGDIR /etc/postgresql/9.1/main/postgresql.log
WORKDIR /usr/lib/postgresql/9.1/bin
USER root
RUN apt-get install -y vim
USER postgres
RUN sed -e '90d' -i /etc/postgresql/9.1/main/pg_hba.conf
RUN sed -e '91d' -i /etc/postgresql/9.1/main/pg_hba.conf
RUN echo "host all all 0.0.0.0/0 trust" >> '/etc/postgresql/9.1/main/pg_hba.conf'
RUN echo "local all all trust" >> '/etc/postgresql/9.1/main/pg_hba.conf'
RUN ./pg_ctl start && sleep 8 && ./createdb pg && ./createdb bloodstone \
&& createuser -Upostgres -s venti \
&& createdb -Uventi -Oventi venti
# ENTRYPOINT ./pg_ctl start && bash -c "while true; do echo "" > /dev/null; sleep 1; done"
VOLUME $PGDATA
EXPOSE 5432
采纳答案by kxxoling
It's merely a misconfiguration. I should have set pg to listen on public addresses or make a port mapping. I fixed this by editing pg config file with sed:
这只是一个错误的配置。我应该设置 pg 来监听公共地址或进行端口映射。我通过用 sed 编辑 pg 配置文件来解决这个问题:
RUN sed -e "s/[#]\?listen_addresses = .*/listen_addresses = '*'/g" -i '/etc/postgresql/9.1/main/postgresql.conf'
Add this to a proper place in your Dockerfile, and you should be OK.
将此添加到 Dockerfile 中的适当位置,您应该没问题。
回答by Curtis Yallop
To troubleshoot postgres auth problems in general, look at the postgres log/stderr to see verbose reasons why it failed. (I see your problem is solved but if anyone runs into similar problems)
要解决一般的 postgres 身份验证问题,请查看 postgres 日志/标准错误以查看失败的详细原因。(我看到您的问题已解决,但如果有人遇到类似问题)
To find the postgres log location: "show log_destination;" if you have a working psql (eg locally on the postgres server box)
查找 postgres 日志位置:“show log_destination;” 如果你有一个可用的 psql(例如在 postgres 服务器上本地)
I see yours is set to "/etc/postgresql/9.1/main/postgresql.log". You can connect to the container to see the log with "docker exec -it container_name bash".
我看到你的设置为“/etc/postgresql/9.1/main/postgresql.log”。您可以连接到容器以查看带有“docker exec -it container_name bash”的日志。
Alternately, if a container runs postgres directly, "docker attach db_container_name" views postgres stderr messages.
或者,如果容器直接运行 postgres,“docker attach db_container_name”会查看 postgres stderr 消息。
Note that by default user postgres does not have a password and uses only ident auth.
请注意,默认情况下,用户 postgres 没有密码,仅使用 ident auth。
回答by prashant
I also faced similar issue. Try running docker-compose up twice.
我也遇到了类似的问题。尝试运行 docker-compose up 两次。
The reason is, that in my docker-compose.yml file the DB service was below the Web service in which it was trying to connect to DB which does not exists yet.
原因是,在我的 docker-compose.yml 文件中,DB 服务位于它试图连接到尚不存在的 DB 的 Web 服务之下。
**
**
version: '2'
services:
nginx:
image: nginx:latest
container_name: ng01
ports:
- "8000:8000"
volumes:
- ./src:/src
- ./config/nginx:/etc/nginx/conf.d
- /static:/static <--- HERE
depends_on:
- web
web:
build: .
container_name: dg01
command: bash -c "python /src/IMS/manage.py makemigrations && python /src/IMS/manage.py migrate && gunicorn IMS.wsgi -b
0.0.0.0:8000"
depends_on:
- db
volumes:
- ./src:/src
- /static:/static
expose:
- "8000"
db:
image: postgres:latest
container_name: ps01**