title: Python Modules for Postgresql tags: python postgresql db # Psycopg See [docs](https://www.psycopg.org/psycopg3/docs/basic/usage.html). To install on ubuntu, `apt install python3-psycopg`, or generally can be done with PIP. Usage: (from the docs) ```py # Note: the module name is psycopg, not psycopg3 import psycopg # Connect to an existing database with psycopg.connect("dbname=test user=postgres") as conn: # Open a cursor to perform database operations with conn.cursor() as cur: # Execute a command: this creates a new table cur.execute(""" CREATE TABLE test ( id serial PRIMARY KEY, num integer, data text) """) # Pass data to fill a query placeholders and let Psycopg perform # the correct conversion (no SQL injections!) cur.execute( "INSERT INTO test (num, data) VALUES (%s, %s)", (100, "abc'def")) # Query the database and obtain data as Python objects. cur.execute("SELECT * FROM test") cur.fetchone() # will return (1, 100, "abc'def") # You can use `cur.fetchmany()`, `cur.fetchall()` to return a list # of several records, or even iterate on the cursor for record in cur: print(record) # Make the changes to the database persistent conn.commit() ```