· 6 years ago · Jun 24, 2019, 12:36 PM
1import psycopg2
2
3DATABASE_URL = 'postgres://.../d7f2u7gjb3nujl'
4
5sql1 = """CREATE TABLE IF NOT EXISTS "test" (id integer, old integer)"""
6sql2 = """SELECT table_name FROM information_schema.tables WHERE table_schema NOT IN ('information_schema','pg_catalog')"""
7sql2_test = "SELECT * FROM information_schema.tables" # for test of access
8
9conn = psycopg2.connect(DATABASE_URL, sslmode='require')
10with conn:
11 cursor = conn.cursor()
12 cursor.execute(sql1)
13 records = cursor.fetchall()
14
15print(records)
16
17###################################################################################
18sql1:
19Traceback (most recent call last):
20 File "main.py", line 12, in <module>
21 records = cursor.fetchall()
22psycopg2.ProgrammingError: no results to fetch
23
24sql2:
25[]
26
27sql2_test:
28[('d7f2u7gjb3nujl', ...]