error installing psycopg2, library not found for -lssl

For anyone looking for a solution for this on macOS Sierra 10.12 (or later, most likely): I fixed this by installing the command line tools: xcode-select –install After that, pip install psycopg2 should work. If it doesn’t, you could also try to link against brew’s openssl: env LDFLAGS=”-I/usr/local/opt/openssl/include -L/usr/local/opt/openssl/lib” pip install psycopg2 with openssl installed … Read more

Passing table name as a parameter in psycopg2

According to the official documentation: If you need to generate dynamically an SQL query (for instance choosing dynamically a table name) you can use the facilities provided by the psycopg2.sql module. The sql module is new in psycopg2 version 2.7. It has the following syntax: from psycopg2 import sql cur.execute( sql.SQL(“insert into {table} values (%s, … Read more

Parameterized queries with psycopg2 / Python DB-API and PostgreSQL

psycopg2 follows the rules for DB-API 2.0 (set down in PEP-249). That means you can call execute method from your cursor object and use the pyformat binding style, and it will do the escaping for you. For example, the following should be safe (and work): cursor.execute(“SELECT * FROM student WHERE last_name = %(lname)s”, {“lname”: “Robert’); … Read more

Pandas read_sql with parameters

The read_sql docs say this params argument can be a list, tuple or dict (see docs). To pass the values in the sql query, there are different syntaxes possible: ?, :1, :name, %s, %(name)s (see PEP249). But not all of these possibilities are supported by all database drivers, which syntax is supported depends on the … Read more