Connect pyodbc to Postgres Connect pyodbc to Postgres postgresql postgresql

Connect pyodbc to Postgres


Since you already have a working DSN defined in odbc.ini you can just use that:

con = pyodbc.connect("DSN=my-connector")

Also, for the record, that extra whitespace in your connection string may have been confusing the issue because this worked fine for me, under Python 2.7 at least

import pyodbcconn_str = (    "DRIVER={PostgreSQL Unicode};"    "DATABASE=postgres;"    "UID=postgres;"    "PWD=whatever;"    "SERVER=localhost;"    "PORT=5432;"    )conn = pyodbc.connect(conn_str)crsr = conn.execute("SELECT 123 AS n")row = crsr.fetchone()print(row)crsr.close()conn.close()