Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect pyodbc to Postgres

Trying to connect to Postgres using pyodbc.

I can connect to the DB with isql:

echo "select 1" | isql -v my-connector

Returns:

+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select 1
+------------+
| ?column?   |
+------------+
| 1          |
+------------+
SQLRowCount returns 1
1 rows fetched

But when I try to connect with pyodbc:

import pyodbc
con = pyodbc.connect("DRIVER={PostgreSQL Unicode}; DATABASE=<dbname>;     UID=<username>; PWD=<password>; SERVER=localhost; PORT=5432;")

I get the following error:

pyodbc.Error: ('08001', '[08001] [unixODBC]connction string lacks some options (202) (SQLDriverConnect)')

obdc.ini file looks like this:

[my-connector]
Description         = PostgreSQL connection to '<dbname>' database
Driver              = PostgreSQL Unicode
Database            = <dbname>
Servername          = localhost
UserName            = <username>
Password            = <password>
Port                = 5432
Protocol            = 9.3
ReadOnly            = No
RowVersioning       = No
ShowSystemTables    = No
ShowOidColumn       = No
FakeOidIndex        = No
ConnSettings        =

odbcinst.ini file looks like this:

[PostgreSQL ANSI]
Description     = PostgreSQL ODBC driver (ANSI version)
Driver          = psqlodbca.so
Setup           = libodbcpsqlS.so
Debug           = 0
CommLog         = 1
UsageCount      = 1

[PostgreSQL Unicode]
Description     = PostgreSQL ODBC driver (Unicode version)
Driver          = psqlodbcw.so
Setup           = libodbcpsqlS.so
Debug           = 0
CommLog         = 1
UsageCount      = 1

Notes:

  • Ubuntu 14.04
  • Python 3
  • Postgresql 9.3

I have used psycopg2 in the past to connect to Postgres, however my current company uses Netezza, Postgres, and MySQL. I want to write 1 connection module, and use different drivers to connect to the different databases. Any help would be greatly appreciated.

-- Thanks

like image 617
Christopher Carlson Avatar asked Dec 22 '15 02:12

Christopher Carlson


People also ask

How do I connect to a Postgres database using Python?

To establish connection with the PostgreSQL database, make sure that you have installed it properly in your system. Open the PostgreSQL shell prompt and pass details like Server, Database, username, and password. If all the details you have given are appropriate, a connection is established with PostgreSQL database.

What is the alternative to Pyodbc?

Another alternative is pypyodbc which was written in pure Python. it can been seen as a re-implemenation of the pyodbc module – with only around 1800 lines code, which is good for maintenance.


Video Answer


1 Answers

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 pyodbc
conn_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()
like image 152
Gord Thompson Avatar answered Oct 10 '22 10:10

Gord Thompson