Setting application_name on Postgres/SQLAlchemy

the answer to this is a combination of: http://initd.org/psycopg/docs/module.html#psycopg2.connect Any other connection parameter supported by the client library/server can be passed either in the connection string or as keywords. The PostgreSQL documentation contains the complete list of the supported parameters. Also note that the same parameters can be passed to the client library using environment … Read more

Python/postgres/psycopg2: getting ID of row just inserted

cursor.execute(“INSERT INTO …. RETURNING id”) id_of_new_row = cursor.fetchone()[0] And please do not build SQL strings containing values manually. You can (and should!) pass values separately, making it unnecessary to escape and SQL injection impossible: sql_string = “INSERT INTO domes_hundred (name,name_slug,status) VALUES (%s,%s,%s) RETURNING id;” cursor.execute(sql_string, (hundred_name, hundred_slug, status)) hundred = cursor.fetchone()[0] See the psycopg docs … Read more

Why are Pandas and GeoPandas able to read a database table using a DBAPI (psycopg2) connection but have to rely on SQLAlchemy to write one?

Probably the main reason why to_sql needs a SQLAlchemy Connectable (Engine or Connection) object is that to_sql needs to be able to create the database table if it does not exist or if it needs to be replaced. Early versions of pandas worked exclusively with DBAPI connections, but I suspect that when they were adding … Read more

Create a Postgres database using python

Use ISOLATION_LEVEL_AUTOCOMMIT, a psycopg2 extensions: No transaction is started when command are issued and no commit() or rollback() is required. import psycopg2 from psycopg2 import sql from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT # <– ADD THIS LINE con = psycopg2.connect(dbname=”postgres”, user=self.user_name, host=””, password=self.password) con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) # <– ADD THIS LINE cur = con.cursor() # Use the psycopg2.sql module … Read more

Python/psycopg2 WHERE IN statement

For the IN operator, you want a tuple instead of list, and remove parentheses from the SQL string. # using psycopg2 data=(‘UK’,’France’) sql=”SELECT * from countries WHERE country IN %s” cur.execute(sql,(data,)) During debugging you can check that the SQL is built correctly with cur.mogrify(sql, (data,))