0

The question may sound stupid but can someone please provide me with pypyodbc with postgresql....? I have been searching it in internet forever and havenot found anything* literally anything.

user3270101
  • 11
  • 1
  • 1

2 Answers2

3

I can't imagine why you'd want to use pypyodbc; look at using a DB-API driver directly instead, such as psycopg2 or the less-well-maintained but more PyPy-friendly pyPgSQL, or the newer less mature py-postgresql. See the database driver list for more info.

If for some reason you need to do something weird and obscure like using ODBC from PyPy to connect to PostgreSQL...

Sometimes, as programmers, we have to do something scary - go off the tutorial track and think about the problem ourselves ;-)

When facing this terrible challenge there are a few tools that greatly ease the process:

  • The documentation for each component we're using; and
  • Tutorials/guides written for some subset of the involved components, but not all of them.

In this case, that suggests that your key resources are:

Some of the examples are the sort of thing you want, but connect to different database engines. You will have to adapt them to PostgreSQL and psqlODBC. The documentation on how each component works will help you do that; for example:

  • Learn how to connect to a DSN using an example that refers to MS SQL Server
  • Learn how to create a DSN in psqlODBC from the psqlODBC docs
  • Combine that knowledge to connect to a psqlODBC DSN

Having learned that pypyodbc is very similar to pyodbc in function, this lets you widen your search for examples covering pyodbc, too:

Now, your task is to synthesize these elements, learning relevant parts from different pieces of documentation, so you can put together a working whole from the individual things you have learned.

Community
  • 1
  • 1
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
2

If you're looking for a very basic example to get you started then the following code works for me:

# -*- coding: utf-8 -*-
import pypyodbc
cnxn = pypyodbc.connect(
    'Driver={PostgreSQL ODBC Driver(UNICODE)};' +
    'Server=localhost;' +
    'Port=5432;' +
    'Database=myDBname;' +
    'Uid=postgres;' +
    'Pwd=whatever;')
crsr = cnxn.cursor()
crsr.execute("SELECT id, customer FROM public.table1")
while 1:
    row = crsr.fetchone()
    if not row:
        break
    print row
crsr.close()
cnxn.close()
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • Hi Gord, may I know how you install `PostgreSQL ODBC Driver(UNICODE)`? Can you post your `/etc/odbcinst.ini` file and `/etc/odbc.ini` file? I am trying to use pypyodbc to connect to postgresql but failed. Thanks. – userpal Oct 04 '14 at 16:09
  • @userpal I did that test on a Windows machine. My Xubuntu box shows a package named `python3-postgresql` in the repositories, so if I was using Python 3 I would probably try that first before messing with a multi-layered ODBC setup. If that won't work for you (e.g. if you are still using Python 2) then try [asking a new question](http://stackoverflow.com/questions/ask). – Gord Thompson Oct 04 '14 at 17:27