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


Requirement already up-to-date: pip in /opt/conda/lib/python3.6/site-packages (10.0.1)
pyspark 2.3.0 requires py4j==0.10.6, which is not installed.
Requirement already satisfied: psycopg2 in /opt/conda/lib/python3.6/site-packages (2.7.4)
pyspark 2.3.0 requires py4j==0.10.6, which is not installed.
Requirement already satisfied: psycopg2-binary in /opt/conda/lib/python3.6/site-packages (2.7.4)
pyspark 2.3.0 requires py4j==0.10.6, which is not installed.

In [ ]:
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));""")
    # run a SELECT statement - no data in there, but we can try it
    cursor.execute("""SELECT * from tutorials""")
    rows = cursor.fetchall()
    print(rows)
except Exception as e:
    print("Uh oh, can't connect. Invalid dbname, user or password?")
    print(e)

In [ ]: