简体   繁体   中英

postgres does not start. (involves Django and Docker)

I do

postgres -D /path/to/data

I get

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

Can someone help me figure out what is going wrong?

When I do

psql -U postgres -h localhost

it works just fine.

Though I need to start postgres to get it running on Docker and Django

Please help

Thanks in advance

Edit:

When I do

docker-compose up 

I get

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

There are actually two problems being displayed here:

The first, when you are attempting to run PostgreSQL by hand, is that you are running it directly in your host operating system, and there is already a copy running there, occupying port 5432.

The second problem, when you are running docker, is that your Django settings are configured to point to a database on localhost . This will work when you are running outside of Docker, with that copy of PostgreSQL running directly in your host operating system, but will not work in Docker, as the web application and database server are running in separate containers.

To take care of the second problem, you simply need to update Django's settings to connect to the host postgres , since this is the name you used for your database container in your docker-compose.yml .

Your problem is the time to launch postgres on docker compose at the same time you are launching django, so is not enough time to the database setup as you can see in your error in connection TCP/IP connections on port 5432? . I have solved this problem running a bash command on docker-compose.yml and create a wait-bd.sh bash script.

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

and in docker-compose.yml add the tag command in the django container:

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

This script gonna wait you database setup, so will run the django setup container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM