Cheat Sheet
Cheat Sheet
DATA ENGINEERING:
Data Modeling with Postgres
READ OPERATIONS
● Methods: fetchall(), fetchmany(), and fetchone()
`psycopg2` MODULE ● Example:
select_query = "SELECT * FROM songs"
● psycopg2 is a PostgreSQL database adapter for
cur.execute(select_query)
the Python programming language
records = cur.fetchmany(5)
● Installation: pip install psycopg2
● Usage: import psycopg2 for record in records:
print(record)
INSERT OPERATIONS
CONNECTING TO A DATABASE
insert_query = "INSERT INTO customer
● conn = psycopg2.connect("host=127.0.0.1 (customer_id, name, rewards) VALUES (%s, %s, %s)"
dbname=studentdb user=student data = (1, "Amanda", True)
password=student")
cur.execute(insert_query, data)
Page 1 of 2
Page 2 of 2