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 172.7.0.2:5432. I’m able to communicate with the server when enter command curl 172.17.0.2:5432 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.

  • How to install jdk, jdeveloper, maven and svn in one docker container?
  • Docker for mac hostname insteat of IP
  • SSH to docker bridged instance
  • Bluemix Container status reads 'Networking'
  • Docker Daemon for Windows
  • How do I write a dockerfile to execute a simple bash script?
  • I’m running these commands in python2 shell in Client Docker Container.

    >>> conn = psycopg2.connect(host='172.17.0.2', 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='172.17.0.2', 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='172.17.0.2', 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.

    Thanks.

  • receiving runtime error on docker container only for specific machine
  • Elastic Beanstalk Single Container Docker - use awslogs logging driver
  • How can I run a Docker container in AWS Elastic Beanstalk with non-default run parameters?
  • Zero Downtime app deployment with CoreOS
  • renaming docker0 virtual bridge name to something else
  • How to add Dockerfile to a docker registry page?
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.