Come installare PostgreSQL e psycopg2 su Osx Snow Leopard
20:29SvliluppoStefano
Ciao a tutti.
Nel mio ultimo post ho mostrato come installare MySQL in Osx a partire dai sorgenti.
Tra i tanti commenti ricevuti, alcuni mi suggerivano di utilizzare l’ottimo homebrew
Consiglio a tutti di utilizzare questo tool per poter installare senza troppa difficoltà svariati pacchetti unix sotto Osx. Tra i pacchetti installabili, c’è anche PostgreSQL, oggetto del post di oggi.
Se volete installare PostgreSQL via homebrew, magari date un occhio a questo post
Il mio post è invece rivolto a chi vuole installare il database a partire dai sorgenti, in /usr/local/postgresql-8.4.4
Passo 1: Configurare la variabile d’ambiente $PATH
Apriamo un terminale ed impostiamo la variabile d’ambiente $PATH in modo da farla puntare alle corrette cartelle all’interno di /usr/local/
1 | mate ~/.profile |
Aggiungiamo, se non esiste già, questa riga in fondo al file .profile:
1 | export PATH="~/bin;/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/pgsql/bin:$PATH" |
e richarichiamo il $PATH lanciando questo metodo:
1 | source ~/.profile |
Per verificare che il nostro $PATH contenga i percorsi impostati sopra digitiamo il seguente comando:
1 | echo $PATH |
Passo 2: Download di PostgreSQL
Creiamo una nuova cartella in cui scaricare i sorgenti e poterli compilare:
1 2 | mkdir ~/src cd ~/src |
Scarichiamo l’ultima versione disponibile al momento della stesura di questo tutorial:
1 | curl -O http://ftp2.it.postgresql.org/mirrors/postgres/source/v8.4.4/postgresql-8.4.4.tar.gz |
Passo 3: Compile and Install
Buildiamo ed installiamo PostgreSQL con i seguenti comandi:
1 2 3 4 5 6 | 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 |
Creiamo un link simbolico, usato in precedenza nel nostro $PATH
1 2 | ln -s /usr/local/postgresql-8.4.4 /usr/local/pgsql mkdir /usr/local/pgsql/data/ |
Creiamo ora un utente postgres, proprietario del server:
1 2 3 4 5 6 7 8 9 10 11 | 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 |
E diamogli i permessi di lettura sulla directory di installazione:
1 | chown -R postgres /usr/local/postgresql-8.4.4/ |
In una nuova console, logghiamoci come postgres e avviamo il server:
1 2 3 | 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 |
Se il server è partito correttamente, creiamo il nostro database test:
1 2 | /usr/local/pgsql/bin/createdb test /usr/local/pgsql/bin/psql test |
Cancelliamo ora le cartelle inutili create durante la creazione dell’utente:
1 2 3 4 5 | sudo su rm -rf /var/home dscl localhost delete /Local/Default/Users/postgres NFSHomeDirectory dscl localhost passwd /Local/Default/Users/postgres exit |
Creiamo ora uno script per avviare e stoppare in automatico il server:
1 2 3 | mkdir bin touch pgsqlscript chmod +x pgsqlscript |
ed editiamo il file appena creato inserendo il seguente corpo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #!/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 $? |
A questo punto sarà possibile avviare il server digitando da console “pgsqlscript start”. Per stopparlo basterà invece digitare “pgsqlscript stop”.
Passo 4: Installiamo i driver Ruby per PostgreSQL
Basterà a questo punto installare la gemma pg per avere accesso al database.
1 | gem install pg |
Passo 4: Installiamo i driver Python per PostgreSQL
Scarichiamo il pacchetto psycopg2
1 2 3 4 | 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 |
e modifichiamo il file setup.cfg con questo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | [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 |
A questo punto possiamo compilare ed installare il pacchetto:
1 | python setup.py install |
Tags: osx, postgreSQL

















[...] ORIGINAL POST [...]
[...] This post was mentioned on Twitter by african-elephant., DevInterface. DevInterface said: "How to install PostgreSQL e psycopg2 on Osx Snow LeopardCome installare PostgreSQL e psycopg2 su Osx Snow Leopard" – http://bit.ly/bcjV6v [...]
[...] http://blog.devinterface.com/2010/10/how-to-install-postgresql-and-psycopg2-on-osx-snow-leopard/ October 8, 2010 // PostgreSQL // No Comments // [...]
Thanks so much for this complete info on OSx