postgres不启动。 (涉及到Django和Docker)

我做

postgres -D /path/to/data 

我明白了

 2017-05-01 16:53:36 CDT LOG: could not bind IPv6 socket: No error 2017-05-01 16:53:36 CDT HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. 2017-05-01 16:53:36 CDT LOG: could not bind IPv4 socket: No error 2017-05-01 16:53:36 CDT HINT: Is another postmaster already running on port 5432? If not, wait a few seconds and retry. 2017-05-01 16:53:36 CDT WARNING: could not create listen socket for "*" 2017-05-01 16:53:36 CDT FATAL: could not create any TCP/IP sockets 

有人能帮我弄清楚什么问题?

当我做

 psql -U postgres -h localhost 

它工作得很好。

虽然我需要启动postgres来让它在Docker和Django上运行

请帮忙

提前致谢

编辑:

当我做

 docker-compose up 

我明白了

 $ docker-compose up Starting asynchttpproxy_postgres_1 Starting asynchttpproxy_web_1 Attaching to asynchttpproxy_postgres_1, asynchttpproxy_web_1 postgres_1 | LOG: database system was interrupted; last known up at 2017- 05-01 21:27:43 UTC postgres_1 | LOG: database system was not properly shut down; automatic recovery in progress postgres_1 | LOG: invalid record length at 0/150F720: wanted 24, got 0 postgres_1 | LOG: redo is not required postgres_1 | LOG: MultiXact member wraparound protections are now enabled postgres_1 | LOG: database system is ready to accept connections web_1 | Performing system checks... web_1 | web_1 | System check identified no issues (0 silenced). web_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f51f88bef28> web_1 | Traceback (most recent call last): web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection web_1 | self.connect() web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 119, in connect web_1 | self.connection = self.get_new_connection(conn_params) web_1 | File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 176, in get_new_connection web_1 | connection = Database.connect(**conn_params) web_1 | File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 164, in connect web_1 | conn = _connect(dsn, connection_factory=connection_factory, async=async) web_1 | psycopg2.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "localhost" (127.0.0.1) and accepting web_1 | TCP/IP connections on port 5432? web_1 | could not connect to server: Cannot assign requested address web_1 | Is the server running on host "localhost" (::1) and accepting web_1 | TCP/IP connections on port 5432? 

实际上在这里显示了两个问题:

首先,当你试图手动运行PostgreSQL的时候,你直接在你的主机操作系统上运行它,并且已经有一个运行在那里的副本,占用5432端口。

第二个问题是,当你运行docker时,你的Django设置被configuration为指向localhost上的一个数据库。 当你在Docker外部运行时,这将会起作用,PostgreSQL的副本直接在主机操作系统中运行,但是在Docker中不起作用,因为Web应用程序和数据库服务器在单独的容器中运行。

为了解决第二个问题,您只需要更新Django的设置来连接到主机postgres ,因为这是您在docker-compose.yml用于数据库容器docker-compose.yml

您的问题是在启动django的同时在postgres上启动postgres的时候,所以没有足够的时间来进行数据库设置,就像您TCP/IP connections on port 5432?连接TCP/IP connections on port 5432?错误中看到的一样TCP/IP connections on port 5432? 。 我已经解决了这个问题在docker-compose.yml上运行bash命令,并创build一个wait-bd.sh bash脚本。

wait-bd.sh

 #!/bin/bash while true; do COUNT_PG=`psql postgresql://username:password@localhost:5432/name_db -c '\l \q' | grep "name_db" | wc -l` if ! [ "$COUNT_PG" -eq "0" ]; then break fi echo "Waiting Database Setup" sleep 10 done 

并在docker-compose.yml中添加tag命令在django容器中:

  django: build: . command: /bin/bash wait-bd.sh 

这个脚本会等待你的数据库设置,所以将运行django安装程序容器。