简体   繁体   中英

psycopg2 on OSX: do I have to install PostgreSQL too?

I want to access a postgreSQL database that's running on a remote machine, from Python in OS/X. Do I have to install postgres on the mac as well? Or will psycopg2 work on its own.

Any hints for a good installation guide for psycopg2 for os/x?

macports tells me that the psycopg2 package has a dependency on the postgres client and libraries (but not the db server). If you successfully installed psycopg, then you should be good to go.

If you haven't installed yet, consider using macports or fink to deal with dependency resolution for you. In most cases, this will make things easier (occasionally build problems erupt).

psycopg2 requires the PostgreSQL libpq libraries and the pg_config utility, which means you need a decent chunk of PostgreSQL to be installed. You could install Postgres and psycopg2 via MacPorts, but the version situation is somewhat messy--you may need to install a newer Python as well, particularly if you want to use a recent version of the PostgreSQL libraries. Depending on what you want to do, for example if you have some other Python you want to use, it may be easier to grab a more standard PostgreSQL install and just build psycopg2 yourself. That's pretty easy if you've already got gcc etc. installed, typically the only build issue is making sure it looks in the right place for the libpq include files. See Getting psycopg2 to work on Mac OS X Leopard and Installing psycopg2 on OS X for a few recipes covering the usual build issues you might run into.

You can install from an OS X PostgreSQL package . Allow it to change your memory settings and reboot (it's reversible by removing '/etc/sysctl.conf') - the README file (which tells you to do this yourself) is out of date. Then use (or get, if you haven't already and) EasyInstall .

Check where the PostgreSQL installer has put things - mine is here:

/Library/PostgreSQL/8.4/

Add this path to your .bash_login or .bash_profile file in your home directory (make one if you don't have it already):

export PATH="/Library/PostgreSQL/8.4/bin:$PATH"

Then (on an Intel iMac running OS 10.4.11 and Python 2.6) do:

sudo easy_install psycopg2

This found psycopg2 2.0.11 and (on my setup) gave the following readout:

warning: no files found matching '*.html' under directory 'doc'
warning: no files found matching 'MANIFEST'
zip_safe flag not set; analyzing archive contents...
Adding psycopg2 2.0.11 to easy-install.pth file

Installed /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/psycopg2-2.0.11-py2.6-macosx-10.3-i386.egg
Processing dependencies for psycopg2
Finished processing dependencies for psycopg2

So I guess I have no psycopg2 documentation... however, despite the warnings, I could then do:

python
>>>import psycopg2
>>>

Success? Perhaps. I haven't tried running anything yet, but getting a successful import was the first goal. BTW this was for Django.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM