XP on Rails Extreme Programming Blog

6Oct/100

How to install PostgreSQL and psycopg2 on Osx Snow Leopard

ORIGINAL POST

Hello everyone.

In my last post I showed how to install MySQL from source on Osx.
Among the many comments received, some of them suggested me to use homebrew

I recommend everyone to use this tool in order to easily install several unix packages on OSX.

Among the packages that can be installed there’s also PostgreSQL, the subject of today’s post.

If you install PostgreSQL via homebrew, maybe have a look at this post

This post is instead directed to those who are willing to install the database from source in /usr/local/postgresql-8.4.4

Step 1: Set the $PATH environment variable

Open a terminal and set the $PATH environment variable in order to link the correct folders in /usr/local/

mate ~/.profile

Add, if it does not exist, this line at the bottom of the .profile file:

export PATH="~/bin;/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/pgsql/bin:$PATH"

and reload the $PATH in this way:

source ~/.profile

To verify that our $PATH contains the paths set above, type the following command:

echo $PATH

Step 2: Download PostgreSQL

Create a new folder to download the sources and compile them:

mkdir ~/src
cd ~/src

Download the latest version available at the time of writing this tutorial:

curl -O http://ftp2.it.postgresql.org/mirrors/postgres/source/v8.4.4/postgresql-8.4.4.tar.gz

Step 3: Compile and Install

Build and install PostgreSQL with the following commands:

tar -zvxf postgresql-8.4.4.tar.gz
rm postgresql-8.4.4.tar.gz
cd postgresql-8.4.4
./configure --prefix=/usr/local/postgresql-8.4.4
ARCH=x86_64 CFLAGS="-arch x86_64" LDFLAGS="-arch x86_64" make
make install

Create a symbilic link, used before in the $PATH

ln -s /usr/local/postgresql-8.4.4 /usr/local/pgsql
mkdir /usr/local/pgsql/data/

Create now a postgres user, owner of the server:

dscl localhost create /Local/Default/Users/postgres
dscl localhost create /Local/Default/Users/postgres PrimaryGroupID 0
dscl localhost create /Local/Default/Users/postgres UniqueID 75
dscl localhost create /Local/Default/Users/postgres UserShell /bin/bash
dscl localhost passwd /Local/Default/Users/postgres
dscl localhost create /Local/Default/Users/postgres NFSHomeDirectory /var/home/postgres
mkdir -p /var/home/postgres
chown -Rf postgres:postgres /var/home/postgres
dscl localhost create /Local/Default/Groups/postgres
dscl localhost create /Local/Default/Groups/postgres UniqueID 75
dscl localhost append /Local/Default/Groups/postgres GroupMembership postgres

And let’s give him read permissions over the installation directory:

chown -R postgres /usr/local/postgresql-8.4.4/

In a new console, log in as postgres and start the server:

su - postgres
/usr/local/pgsql/bin/initdb -E UTF8 -D /usr/local/pgsql/data/
/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/postgresql.log start

If the server is started correctly, we can create our test database:

createdb test
psql test

Now delete unnecessary folders created when creating the user:

sudo su
rm -rf /var/home
dscl localhost delete /Local/Default/Users/postgres NFSHomeDirectory
dscl localhost passwd /Local/Default/Users/postgres
exit

Then we can create scripts to start and stop the PostgreSQL server using the command line.

We create a bin folder in our home and the following file, also giving execute permissions:

mkdir bin
touch pgsqlscript
chmod +x pgsqlscript

Edit the file you’ve just created by inserting the following body:

#!/bin/bash

start()
{
        echo -n "Starting PostgreSQL server"
        sudo su postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ -l /usr/local/pgsql/data/postgresql.log start'
       
        return
}

stop()
{
        echo -n "Stopping PostgreSQL server"
        sudo su postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data/ stop'
        return
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: {start|stop|restart}"
        exit 1
        ;;
esac
exit $?

At this point you can start the server by typing “pgsqlscript start” in the console. To stop the server just type “pgsqlscript stop”.

Step 4: Install Ruby PostgreSQL drivers

Just install the pg gem to have access to the database.

gem install pg

Step 5: Install Python drivers for PostgreSQL

Download the psycopg2 package

cd ~/src
curl -O http://initd.org/psycopg/tarballs/psycopg2-2.2.2.tar.gz
tar xzfv psycopg2-2.2.2.tar.gz
cd psycopg2-2.2.2

and modify the setup.cfg file with this one:

[build_ext]
define=PSYCOPG_EXTENSIONS,PSYCOPG_NEW_BOOLEAN,HAVE_PQFREEMEM,HAVE_PQPROTOCOL3

# PSYCOPG_EXTENSIONS enables extensions to PEP-249 (you really want this)
# PSYCOPG_DISPLAY_SIZE enable display size calculation (a little slower)
# HAVE_PQFREEMEM should be defined on PostgreSQL >= 7.4
# HAVE_PQPROTOCOL3 should be defined on PostgreSQL >= 7.4
# PSYCOPG_DEBUG can be added to enable verbose debug information
# PSYCOPG_OWN_QUOTING can be added, but it is deprecated (will go away in 2.1)
# PSYCOPG_NEW_BOOLEAN to format booleans as true/false vs 't'/'f'

# Set to 1 to use Python datatime objects for default date/time representation.
use_pydatetime=1

# If the build system does not find the mx.DateTime headers, try
# uncommenting the following line and setting its value to the right path.
#mx_include_dir=

# For Windows only:
# Set to 1 if the PostgreSQL library was built with OpenSSL.
# Required to link in OpenSSL libraries and dependencies.
have_ssl=0

# Statically link against the postgresql client library.
static_libpq=0

# "pg_config" is the preferred method to locate PostgreSQL headers and
# libraries needed to build psycopg2. If pg_config is not in the path or
# is installed under a different name uncomment the following option and
# set it to the pg_config full path.
pg_config=/usr/local/pgsql/bin/pg_config

# If "pg_config" is not available, "include_dirs" can be used to locate
# postgresql headers and libraries. Some extra checks on sys.platform will
# still be done in setup.py.
# The next line is the default as used on psycopg author Debian laptop:
#include_dirs=/usr/local/lib

# Uncomment next line on Mandrake 10.x (and comment previous ones):
#include_dirs=/usr/include/pgsql/8.0:/usr/include/pgsql/8.0/server

# Uncomment next line on SUSE 9.3 (and comment previous ones):
#include_dirs=/usr/include/pgsql:/usr/include/pgsql/server

# If postgresql is installed somewhere weird (i.e., not in your runtime library
# path like /usr/lib), just add the right path in "library_dirs" and any extra
# libraries required to link in "libraries".
library_dirs=/usr/local/pgsql/lib
libraries=/usr/lib

At this point we can compile and install the package:

python setup.py install
Tagged as: , No Comments
13Sep/101

How to install MySQL and Rails on Osx Snow Leopard

ORIGINAL POST

Hello everyone.

These days I’ve bought a MacBook Pro with Osx Snow Leopard 64-bit pre installed.

Having to develop mainly in Rails and Django on MySQL and PostgreSQL, I’ve found some difficulties to install and properly configure all packages.

The main problems were due to the presence of 32bit libraries and other at 64bit. For example, Python or Ruby (already installed by default) were compiled at 32bit. Everything seems to work best, however, until you try to install the gem “mysql” or the eggs “psycopg2″ or “MySQL_python”.

These database drivers, having to be “compiled”, generate many problems of incompatibility between different architectures.

In this first tutorial, we’ll see how to install MySQL and Rails 3.0. In the next we’ll see how to proceed with Django and PostgreSQL.

The approaches taken by me are simply the result of googling and other online tutorials. So I’ll link every resource I’ve used.

Regarding the installation of MySQL, I’ve mainly followed the Hivelogic tutorial.

See below the key points that allowed a successful installation.

Step 1: Set the $PATH environment variable

Open a terminal and set the $PATH environment variable in order to link the correct folders in /usr/local/

mate ~/.profile

Add, if it does not exist, this line at the bottom of the .profile file:

export PATH="~/bin;/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

and reload the $PATH in this way:

source ~/.profile

To verify that our $PATH contains the paths set above, type the following command:

echo $PATH

Step 2: Download MySQL

Create a new folder to download the sources and compile them:

mkdir ~/src
cd ~/src

Download the latest version available at the time of writing this tutorial:

curl -O http://mysql.mirrors.pair.com/Downloads/MySQL-5.1/mysql-5.1.47.tar.gz

Step 3: Compile and Install

Build and install MySQL with the following commands:

tar xzvf mysql-5.1.47.tar.gz
cd mysql-5.1.47
./configure --prefix=/usr/local/mysql --with-extra-charsets=complex \
--enable-thread-safe-client --enable-local-infile --enable-shared \
--with-plugins=innobase

make
sudo make install

At this point we create a mysql user and a default database.

cd /usr/local/mysql
sudo ./bin/mysql_install_db --user=mysql
sudo chown -R mysql ./var
cd ..

Then we can create scripts to start and stop the MySQL server using the command line.

We create a bin folder in our home and the following file, also giving execute permissions:

mkdir bin
touch mysqlscript
chmod +x mysqlscript

Edit the file you’ve just created by inserting the following body:

#!/bin/bash

start()
{
        echo -n "Starting MysSQL server"
        cd /usr/local/mysql ; sudo /usr/local/mysql/bin/mysqld_safe        
        return
}

stop()
{
        echo -n "Stopping MySQL server"
        cd /usr/local/mysql ; sudo /usr/local/mysql/bin/mysqladmin -u root -p shutdown
        return
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: {start|stop|restart}"
        exit 1
        ;;
esac
exit $?

At this point you can start the server by typing “mysqlsript start” in the console. To stop the server just type “mysqlscript stop”.

We are now able to install Ruby on Rails. As mentioned at the beginning of the article, system Ruby is compiled at 32bit.
If you want to compile from source at 64bit and place it in /usr/local/ ruby just follow this tutorial.

Since it is my intention to use instead rvm, I’ll settle for a system level Ruby at 32bit and I prefer to compile every new virtual environment at 64bit (from this tutorial)

Therefore proceed as follows:

Step 1: Install rvm

Launch in a console this command and follow the instructions:

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

Step 2: Install Ruby 1.9.2

Type the following commands and verify that you have version 1.9.2 installed.

rvm install 1.9.2 -C --with-readline-dir=/opt/local,--build=x86_64-apple-darwin10
rvm 1.9.2
ruby -v

Step 3: Create a Rails3 gemset

Create a Rails3 gemset and activate it as default:

rvm use --create 1.9.2@rails3
rvm 1.9.2@rails3 --default

Step 4: Install Rails3 and database drivers for SQLite and MySQL

gem install sqlite3-ruby
env ARCHFLAGS="-arch x86_64" gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
gem install rails

In this tutorial we’ve then installed MySQL and Ruby 1.9.2 from source. Now that all the packages have been compiled to 64bit we should not find errors while installing other application’s gems.