Postgresql 如何使Postgres(Docker)可以远程访问任何IP?

Postgresql 如何使Postgres(Docker)可以远程访问任何IP?,postgresql,docker,containers,dockerfile,Postgresql,Docker,Containers,Dockerfile,我正在使用以下命令创建PostgreSQL容器: docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 -d postgres:9.3.6 它将下载所需的基本映像,并创建一个容器 当我使用telnet检查服务连接时,我得到: $ telnet 127.0.0.1 5432 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. 它使用127.0.0.1

我正在使用以下命令创建PostgreSQL容器:

docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 -d postgres:9.3.6
它将下载所需的基本映像,并创建一个容器

当我使用telnet检查服务连接时,我得到:

$ telnet 127.0.0.1 5432

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
它使用
127.0.0.1
作为IP地址可以正常工作,但是如果我使用计算机的IP地址,控制台就会卡住,很长一段时间后会出现超时错误

$ telnet 191.115.52.110 5432

Trying 191.115.52.110...
telnet: Unable to connect to remote host: Connection timed out
我应该怎么做才能从任何IP访问我的PostgreSQL容器

我尝试过这样传递一个config属性。但是,当我这样做时,容器会立即退出(可能会崩溃)

当我执行
show listen\u addresses
以获取listen\u addresses运行时属性时,我得到了
*
,这意味着参数被正确地设置为
*
,但仍然不起作用

postgres=# show listen_addresses;
 listen_addresses 
------------------
 *
(1 row)

我记得,基本的官方postgresql docker映像只允许本地连接,这意味着config
listen\u addresses='127.0.0.1'

要修复它,请进入容器内部,更新文件
postgresql.conf
配置
listen\u addresses='*'
,然后重新启动容器。

您必须创建带有参数的postgresql.conf,并设置listen\u addresses='*'

启动容器时请连接

docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 \
 -d postgres:9.3.6 \
 -c config_file=/path/to/postgresql.conf
下一个解决方案。 创建Dockerfile并添加以下内容:

FROM ubuntu

# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8

# Add PostgreSQL's repository. It contains the most recent stable release
#     of PostgreSQL, ``9.3``.
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list

# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
#  There are some warnings (in red) that show up during the build. You can hide
#  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3

# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
# after each ``apt-get``

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# Note: here we use ``&&\`` to run commands one after the other - the ``\``
#       allows the RUN command to span multiple lines.
RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker

# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf

# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf

# Expose the PostgreSQL port
EXPOSE 5432

# Add VOLUMEs to allow backup of config, logs and databases
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
从Dockerfile生成映像并为其指定名称

$ docker build -t my_postgresql .
运行PostgreSQL server容器(在前台):

从主机系统连接 $docker ps

CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                      NAMES
5e24362f27f6        my_postgresql:latest   /usr/lib/postgresql/   About an hour ago   Up About an hour    0.0.0.0:49153->5432/tcp                    pg_test

$ psql -h localhost -p 49153 -d docker -U docker --password

我成功了,我必须打开路由器的端口。考虑到我以前做过这件事,我犯了一个愚蠢的错误,但不知怎么的,今天我忘了。所有其他答案都是正确的,因为
listen\u地址
需要是
*


为了检查是否是,您可以执行
show listen\u addressespostgres=#
控制台中运行code>。

如果在没有
-d
的情况下运行相同的命令,您至少可以看到错误消息,以便进行故障排除(下面的答案几乎肯定是实际问题,只是认为这会有帮助)“侦听地址='*'”不能包含空格字符这只会引发错误(在没有
-d
的情况下执行时):
/docker-entrypoint.sh:line 73:exec:config\u file=postgresql.conf:not found
,因为您必须创建此文件并确定properties@SeniorPomidor您能在下面的命令中确认“U”是用户名吗?psql-h localhost-p 49153-d docker-U docker--password@SukanyaPai
-U
表示
username
,或者您可以编写完整的参数
--username=username
$ docker run --rm -P --name pg_test my_postgresql
CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                      NAMES
5e24362f27f6        my_postgresql:latest   /usr/lib/postgresql/   About an hour ago   Up About an hour    0.0.0.0:49153->5432/tcp                    pg_test

$ psql -h localhost -p 49153 -d docker -U docker --password