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.

  • docker-compose --x-networking up not working
  • php significant slower on docker then kvm
  • Newer versions of docker have --cap-add, what CAP's can be added?
  • docker is not VM , why container need base image OS ? [closed]
  • Docker Debian install fails
  • Extra characters appearing in Flask JavaScript files
  • 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.

  • why my pipework does't add a interface in my container?
  • permission issues with apache inside docker
  • How to write docker file to run a docker run command inside an image
  • Monitoring changes at mounted a host directory from Docker container
  • error 7#7: *1 upstream prematurely closed connection while reading response header from upstream
  • Create django super user in a docker container without inputting password
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.