In [1]:
!pip install --upgrade pip
!pip install psycopg2
!pip install psycopg2-binary


Collecting pip
  Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
    100% |████████████████████████████████| 1.3MB 565kB/s ta 0:00:011
Installing collected packages: pip
  Found existing installation: pip 9.0.3
    Uninstalling pip-9.0.3:
      Successfully uninstalled pip-9.0.3
Successfully installed pip-18.0
Collecting psycopg2
  Downloading https://files.pythonhosted.org/packages/5e/d0/9e2b3ed43001ebed45caf56d5bb9d44ed3ebd68e12b87845bfa7bcd46250/psycopg2-2.7.5-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
    100% |████████████████████████████████| 2.7MB 6.8MB/s eta 0:00:01
Installing collected packages: psycopg2
Successfully installed psycopg2-2.7.5
Collecting psycopg2-binary
  Downloading https://files.pythonhosted.org/packages/3f/4e/b9a5cb7c7451029f67f93426cbb5f5bebedc3f9a8b0a470de7d0d7883602/psycopg2_binary-2.7.5-cp36-cp36m-manylinux1_x86_64.whl (2.7MB)
    100% |████████████████████████████████| 2.7MB 8.4MB/s eta 0:00:01    89% |████████████████████████████▉   | 2.4MB 10.9MB/s eta 0:00:01
Installing collected packages: psycopg2-binary
Successfully installed psycopg2-binary-2.7.5

In [3]:
import psycopg2

try:
    connect_str = "dbname='basic_db' user='gpadmin' host='gpdbsne' " + \
                  "password='pivotal'"
    # use our connection values to establish a connection
    conn = psycopg2.connect(connect_str)
    # create a psycopg2 cursor that can execute queries
    cursor = conn.cursor()
    # create a new table with a single column called "name"
    #cursor.execute("""CREATE TABLE tutorials (name char(40));""")
    
    #cursor.execute("""select * from tutorials;""")
    
    # run a SELECT statement - no data in there, but we can try it
    cursor.execute("""SELECT count(*) from basictable""")
    rows = cursor.fetchall()
    print(rows)
except Exception as e:
    print("Uh oh, can't connect. Invalid dbname, user or password?")
    print(e)


Uh oh, can't connect. Invalid dbname, user or password?
FATAL:  database "basic_db" does not exist


In [ ]:


In [ ]: