Unable to execute Postgresql commands with python in seperate docker containers

I have this setup
1) Docker Container Server running postgresql image in background created with command
docker run --name post2 -d postgres
2) Client Docker Container running ubuntu with python running psycopg2
My Postgresql container is exposed on ip address I’m able to communicate with the server when enter command curl in my client container

The Docker Container Server logs LOG:invalid length of startup packet. I do not send any message packets but it shows communication with server.

  • Docker: Can't pick up config file for nginx on Windows 10
  • What will happen after the maximum number of images pushed to ECR repository
  • Docker exec npm command
  • How to open a port from a running docker conatiner
  • How to do a custom hostname (URL) to site in docker container in Ubuntu 16.04?
  • No module named django error when running any manage.py command in docker with docker-compose
  • I’m running these commands in python2 shell in Client Docker Container.

    >>> conn = psycopg2.connect(host='', port='5432', user='postgres', database='yoman')
    >>> cur = conn.cursor()
    >>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")
    >>> conn1 = psycopg2.connect(host='', port='5432', user='postgres', database='template1')

    yeoman and template1 are precreated databases in the server which I created.
    I believe its successfully communicating with server because it recognizes that the database asdf does not exist as stack trace below.

     >>> conn = psycopg2.connect(host='', port='5432', user='postgres', database='asdfd')
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 164, in connect
            conn = _connect(dsn, connection_factory=connection_factory, async=async)
        psycopg2.OperationalError: FATAL:  database "asdfd" does not exist

    My problem is my commands are not executing in the database.
    I run the code below to create a table called “test” but nothing happens. Also no error is thrown.
    cur.execute("CREATE TABLE test (id serial PRIMARY KEY, num integer, data varchar);")

    My Postgres database which shows no tables.

    postgres=# \l
                                     List of databases
       Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
     postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
     template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
               |          |          |            |            | postgres=CTc/postgres
     template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
               |          |          |            |            | postgres=CTc/postgres
     yoman     | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
    (4 rows)
    postgres=# \c yoman
    You are now connected to database "yoman" as user "postgres".
    yoman=# \d
    No relations found.

    Appreciate any help on this.


  • How to increase base size of one docker container
  • Tomcat not running on docker image
  • List containers in the same docker-compose
  • Remote Debugging From Local to Remote Host Docker Container
  • Docker exits with a exit code on starting
  • Access a docker containers own localhost inside that container — php apache
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.