frank
frank

Reputation: 101

docker container connect local postgresql

I run python container, I want to connect localhost postegresql. And I try some method. But not work. Please talk me. How can I do ? Thanks.

I have run postegresql on port 5432, create datatbase and grant user.

run docker comand

docker run --name=python3 -v ${pwd}:/code -w /code python

python code

import psycopg2

def main():
    #Define our connection string
    conn_string = "host='localhost' dbname='testDB' user='test' password='test'"

    # print the connection string we will use to connect
    print ("Connecting to database\n    ->{}".format(conn_string))

    # get a connection, if a connect cannot be made an exception will be raised here
    conn = psycopg2.connect(conn_string)

    # conn.cursor will return a cursor object, you can use this cursor to perform queries
    cursor = conn.cursor()
    print ("Connected!\n")

if __name__ == "__main__":
    main()

error message

Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

Upvotes: 4

Views: 12897

Answers (3)

speedy
speedy

Reputation: 1

to fix this bug:

First, it is normal that it does not work, because postgresql does not run in the same container as the application so the host localhost: 5432 does not exist. to fix it : on the properties file isntead localhost:5432 use your IPadresse like IP:5432 and in the pg_hba.conf add this host all all 0.0.0.0/0 md5

Upvotes: 0

samprog
samprog

Reputation: 2644

Reading that you're on Windows 10 and running postgresql on the host, I advise you to run postgresql in a container. It makes this way easier.

To connect the python container to the postgres container you'll need a docker network though. Let's call it postgres_backend.

docker network create postgres_backend

You can create the postgresql container with the following command. Just change the /path/to/store/data to a local directory in which you'd like to store the postgres data:

docker run --name postgres \
  -e POSTGRES_PASSWORD=test \
  -e POSTGRES_USER=test \
  -d --restart always \
  -v /path/to/store/data:/var/lib/postgresql/data \
  --net postgres_backend \
  postgres:9.6

Now your postresql container should be up and running :)

To connect your python container to it, you'll have to add a --net postgres_backend to your docker run command and change the host in your script to "postgres" (it's the name we gave the postgres container with --name).

If the python container can't find the host "postgres", try it with the IP shown when entering the command docker exec -ti postgres ip addr show.

Upvotes: 1

VonC
VonC

Reputation: 1328202

It depends on your host OS and your docker version.
I assume here your database is not running in a container itself, but rather on the host itself (localhost).

For instance, as mentioned in "From inside of a Docker container, how do I connect to the localhost of the machine?", with Docker for Mac v 17.06 and above (June 2017), you can connect to the special Mac-only DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.

On Linux directly, you would use the host mode, with an image having ifconfig installed:

docker run --network="host" -id <Docker image ID>

Upvotes: 5

Related Questions