4

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?

frank
  • 101
  • 1
  • 2
  • 6

3 Answers3

5

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>
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Hi, I try `docker run --name=python3 -v ${pwd}:/code -w /code --network="host" python`, but not work. My OS is Wiindows 10. database is run on local machine. not container. Thanks. Will I still need to do anything? – frank Jul 27 '17 at 05:19
  • @frank For Windows, you can use the bridge network, and pass the Hyper-V IP address: https://stackoverflow.com/a/41077351/6309 – VonC Jul 27 '17 at 07:06
  • 1
    @frank Note: the other answer is if you are running postgreSQL inside its own container, which was not your question. – VonC Jul 27 '17 at 07:07
  • 1
    @frank, it will be useful to edit your question pointing out that Postgres is running on a virtual machine on top of Windows. It is not running on localhost as you said first – Robert Jul 27 '17 at 12:12
1

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.

samprog
  • 2,454
  • 1
  • 13
  • 18
  • Hi, Thank you for your solution. I try this method and It works, but I just want to connect local database, Thank you. – frank Jul 27 '17 at 13:16
  • Maybe this can be of more help: https://stackoverflow.com/questions/27556301/how-to-connect-to-local-mysql-server-through-docker – samprog Jul 28 '17 at 06:28
0

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

speedy
  • 1