Wednesday 24 December 2014

Psycopg2 - MacOS X - library not loaded libssl.1.0.0.dylib reason image not found - Issue Resolved

I encountered following error on my MacOS X after installing PostgreSQL and Psycopg2 for my Python and Django based application.

ImportError: dlopen(/Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/psycopg2/, 2): Library not loaded: libssl.1.0.0.dylib
  Referenced from: /Users/aliraza/Projects/VirtualEnv/django1.5/lib/python2.7/site-packages/psycopg2/
  Reason: image not found

Check if you have openssl installed by writing openssl on the commandline:
$ openssl

 If it starts, this means its installed in your system and you may skip STEP-2 and go directly to STEP-3.

If openssl isn't installed, install it by:
$ brew install openssl

Check the path of openssl lib on your machine. On my machine it was:
$ /usr/local/opt/openssl/lib/

You have to simply include this path from STEP-3 by running following command:
i.e in my case, the command with the STEP-3 path is:
$ sudo export DYLD_LIBRARY_PATH=/usr/local/opt/openssl/lib/

You may also have to create a symbolic link to PostgreSQL's lib folder. For that, first check your path of PostgreSQL's lib folder and then write following commands accordingly:

$ sudo ln -s /Library/PostgreSQL/9.4/lib/libssl.1.0.0.dylib /usr/lib/
$ sudo ln -s /Library/PostgreSQL/9.4/lib/libcrypto.1.0.0.dylib /usr/lib/

Now run python/ Django shell and import psycopg2. I had no errors after following this recipe.

Tuesday 25 March 2014

PostgreSQL - pg_dump to copy PostgreSQL Database From One Remote Server to Another

While involved in Python + Django based projects and using postgreSQL as a database backend, there are occasions where I need to switch between servers and at times need to copy databases as well. For transferring the django application data + schema from one remote server to another, I use following ways as per convenience, available options, server restrictions etc.

1) Copying Django application data + schema (PostgreSQL) from one remote server by dumping the data in a file and then transferring that file to another remote server and restoring it. In this approach, you have got three options:

  • Dump both schema as well as Data to the file:
pg_dump -C -h host -U username db_name > /any_directory/dump_schema_and_data_file 
  • Dump only schema from PostgreSQL Database to a file:
pg_dump -Cs -h host -U username db_name > /any_directory/dump_schema_file
  • Dump only data from PostgreSQL Database to file:
pg_dump -a -h host -U username db_name > /any_directory/dump_data_file

After copying the above file to target server, you can easily restore database from file by using following command:
psql -h host -U username db_name < data_schema_file

2) Second approach I use to copy PostgreSQL database from one remote server to another is by directly using following command i.e without copying the data to intermediate file:

pg_dump -C -h localhost -U localuser db_name | psql remotehost -U remoteuser db_name