tzenderman
tzenderman

Reputation: 2673

Postgresql Psycopg2

I am a total programming n00b and just starting to learn django. I've been able to get things done with SQLite, but I have been at a complete standstill with PostgreSQL... I've been looking through Stackoverflow and other places for hours and have been unable to find a good answer.

My settings.py file:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'mydb',
        'USER': 'user',
        'PASSWORD': 'passwd',
        'HOST': 'localhost',
        'PORT': '',
}

}

Command run in the terminal:

$ python manage.py validate    

Terminal Output:

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
return self.handle_noargs(**options)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/commands/validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/validation.py", line 23, in get_validation_errors
from django.db import models, connection
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 40, in <module>
backend = load_backend(connection.settings_dict['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
backend = load_backend(db['ENGINE'])
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
return import_module('.base', backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 13, in <module>
from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/creation.py", line 1, in <module>
import psycopg2.extensions
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/__init__.py", line 67, in <module>
from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so, 2): Symbol not found: _PQbackendPID
Referenced from: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/psycopg2/_psycopg.so
Expected in: dynamic lookup

Upvotes: 1

Views: 3368

Answers (2)

Jonathan Vanasco
Jonathan Vanasco

Reputation: 15690

By default, psycopg2 will not compile right on OSX.

The maintainers suggest you use a version from Fink or Macports if possible ( http://initd.org/psycopg/install/ ).

If you want to / need to install from source, they offer these links that get around common issues: http://initd.org/psycopg/articles/2010/11/11/links-about-building-psycopg-mac-os-x/

IIRC, the best way to handle this is:

  • setup environment vars to ensure they pick up the right pg_config binary if you do a pip/easy_install ( it has all the config info for the postgresql client/server you're building against ) or manually note it in the setup.cfg if you manually build.

  • setup the environment vars to have the right ARCHFLAGS explicitly set for your osx install. sometimes python will try to build 64bit on a 32bit install or vice versa.

  • new installs have had people run into issues with libssl , i never encountered those

Upvotes: 1

Chris Travers
Chris Travers

Reputation: 26464

Reading the error, I dont think this is a case of psycopg2 not being installed but due to it not loading correctly. Something is wrong here.

I would make sure you have libpq installed (postgresql client libraries), plus the relevant header files. then I would remove psycopg2 and try to install again.

If that fails, make sure the header files are for the same version of libpq as you have installed. It sounds like you are having issues with expected symbol exports not being there, which suggests possible library versioning issues.

Upvotes: 0

Related Questions