Installing ruby-pg on MAC Snow Leopard

Spent almost 2hrs in figuring how to get this done successfully. The error messages are so cryptic.

First of all, make sure u have postgres installed on your machine and the path to the postgres bin folder is in the $PATH enviroment variable. You can follow http://anooj.me/blog/?p=64 to set PATH.

To confirm, type pg_config in terminal to see if its in PATH. It should display the pg related configuration


anooj.n@dv-mac-anooj-n ~$ pg_config
BINDIR = /Library/PostgreSQL/8.4/bin
DOCDIR = /Library/PostgreSQL/8.4/doc/postgresql
HTMLDIR = /Library/PostgreSQL/8.4/doc/postgresql
INCLUDEDIR = /Library/PostgreSQL/8.4/include
PKGINCLUDEDIR = /Library/PostgreSQL/8.4/include/postgresql
INCLUDEDIR-SERVER = /Library/PostgreSQL/8.4/include/postgresql/server
LIBDIR = /Library/PostgreSQL/8.4/lib
PKGLIBDIR = /Library/PostgreSQL/8.4/lib/postgresql
LOCALEDIR = /Library/PostgreSQL/8.4/share/locale
MANDIR = /Library/PostgreSQL/8.4/share/man
SHAREDIR = /Library/PostgreSQL/8.4/share/postgresql
SYSCONFDIR = /Library/PostgreSQL/8.4/etc/postgresql
PGXS = /Library/PostgreSQL/8.4/lib/postgresql/pgxs/src/makefiles/pgxs.mk
CONFIGURE = '--prefix=/Users/buildfarm/pginstaller/server/staging/osx' '--with-openssl' '--with-perl' '--with-python' '--with-tcl' '--with-bonjour' '--with-pam' '--with-krb5' '--enable-thread-safety' '--with-libxml' '--with-ossp-uuid' '--with-includes=/usr/local/include/libxml2:/usr/local/include' '--docdir=/Users/buildfarm/pginstaller/server/staging/osx/doc/postgresql' '--with-libxslt' 'CFLAGS=-isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 -headerpad_max_install_names -arch ppc -arch i386' 'LDFLAGS=-L/usr/local/lib'
CC = gcc -no-cpp-precomp
CPPFLAGS = -I/usr/include/libxml2 -I/usr/local/include/libxml2 -I/usr/local/include
CFLAGS = -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 -headerpad_max_install_names -arch ppc -arch i386 -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -fno-strict-aliasing -fwrapv
CFLAGS_SL =
LDFLAGS = -L/usr/local/lib -L/usr/lib
LDFLAGS_SL =
LIBS = -lpgport -lxslt -lxml2 -lpam -lssl -lcrypto -lkrb5 -lz -lreadline -lm
VERSION = PostgreSQL 8.4.8

Initially went straight and fired:


anooj.n@dv-mac-anooj-n ~$ sudo gem install ruby-pg
Building native extensions. This could take a while...
ERROR: Error installing ruby-pg:
ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb –with-pg-config=/Library/PostgreSQL/8.4/bin/pg_config
checking for main() in -lpq… yes
checking for libpq-fe.h… yes
checking for libpq/libpq-fs.h… yes
checking for PQconnectionUsedPassword()… no
checking for PQisthreadsafe()… no
checking for PQprepare()… no
checking for PQexecParams()… no
checking for PQescapeString()… no
checking for PQescapeStringConn()… no
checking for lo_create()… no
checking for pg_encoding_to_char()… no
checking for PQsetClientEncoding()… no
creating Makefile

make
gcc -I. -I. -I/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0 -I. -DHAVE_LIBPQ_FE_H -DHAVE_LIBPQ_LIBPQ_FS_H -I/Library/PostgreSQL/8.4/include -D_XOPEN_SOURCE -D_DARWIN_C_SOURCE -fno-common -arch x86_64 -g -Os -pipe -fno-common -DENABLE_DTRACE -fno-common -pipe -fno-common -c compat.c
In file included from compat.c:16:
compat.h:38:2: error: #error PostgreSQL client version too old, requires 7.3 or later.
In file included from compat.c:16:
compat.h:69: error: conflicting types for ‘PQconnectionNeedsPassword’
/Library/PostgreSQL/8.4/include/libpq-fe.h:293: error: previous declaration of ‘PQconnectionNeedsPassword’ was here
compat.h:70: error: conflicting types for ‘PQconnectionUsedPassword’
/Library/PostgreSQL/8.4/include/libpq-fe.h:294: error: previous declaration of ‘PQconnectionUsedPassword’ was here
compat.h:123: error: redeclaration of enumerator ‘PQERRORS_TERSE’
/Library/PostgreSQL/8.4/include/libpq-fe.h:105: error: previous definition of ‘PQERRORS_TERSE’ was here
compat.h:124: error: redeclaration of enumerator ‘PQERRORS_DEFAULT’
/Library/PostgreSQL/8.4/include/libpq-fe.h:106: error: previous definition of ‘PQERRORS_DEFAULT’ was here
compat.h:126: error: redeclaration of enumerator ‘PQERRORS_VERBOSE’
/Library/PostgreSQL/8.4/include/libpq-fe.h:108: error: previous definition of ‘PQERRORS_VERBOSE’ was here
compat.h:126: error: conflicting types for ‘PGVerbosity’
/Library/PostgreSQL/8.4/include/libpq-fe.h:108: error: previous declaration of ‘PGVerbosity’ was here
compat.h:130: error: redeclaration of enumerator ‘PQTRANS_IDLE’
/Library/PostgreSQL/8.4/include/libpq-fe.h:96: error: previous definition of ‘PQTRANS_IDLE’ was here
compat.h:131: error: redeclaration of enumerator ‘PQTRANS_ACTIVE’
/Library/PostgreSQL/8.4/include/libpq-fe.h:97: error: previous definition of ‘PQTRANS_ACTIVE’ was here
compat.h:132: error: redeclaration of enumerator ‘PQTRANS_INTRANS’
/Library/PostgreSQL/8.4/include/libpq-fe.h:98: error: previous definition of ‘PQTRANS_INTRANS’ was here
compat.h:133: error: redeclaration of enumerator ‘PQTRANS_INERROR’
/Library/PostgreSQL/8.4/include/libpq-fe.h:99: error: previous definition of ‘PQTRANS_INERROR’ was here
compat.h:135: error: redeclaration of enumerator ‘PQTRANS_UNKNOWN’
/Library/PostgreSQL/8.4/include/libpq-fe.h:101: error: previous definition of ‘PQTRANS_UNKNOWN’ was here
compat.h:135: error: conflicting types for ‘PGTransactionStatusType’
/Library/PostgreSQL/8.4/include/libpq-fe.h:101: error: previous declaration of ‘PGTransactionStatusType’ was here
compat.h:140: error: conflicting types for ‘PQtransactionStatus’
/Library/PostgreSQL/8.4/include/libpq-fe.h:285: error: previous declaration of ‘PQtransactionStatus’ was here
compat.h:141: error: conflicting types for ‘PQparameterStatus’
/Library/PostgreSQL/8.4/include/libpq-fe.h:287: error: previous declaration of ‘PQparameterStatus’ was here
compat.h:155: error: conflicting types for ‘PQsetErrorVerbosity’
/Library/PostgreSQL/8.4/include/libpq-fe.h:309: error: previous declaration of ‘PQsetErrorVerbosity’ was here
compat.h:165:2: error: #error unsupported postgresql version, requires 7.3 or later.
compat.c: In function ‘PQsetClientEncoding’:
compat.c:21: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:28: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:35: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:43: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:49: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:55: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:61: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:67: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:73: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:79: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:85: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:93: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:102: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:109: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:115: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:125: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:131: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:137: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:143: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:151: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:159: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:167: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:173: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:179: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:185: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:191: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:197: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:203: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:209: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:218: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:225: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:268: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:327: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:374: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:453: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
compat.c:527: error: old-style parameter declarations in prototyped function definition
compat.c:527: error: expected ‘{’ at end of input
make: *** [compat.o] Error 1

Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/ruby-pg-0.7.9.2008.01.28 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/ruby-pg-0.7.9.2008.01.28/ext/gem_make.out

The error mentioned something like "PostgreSQL client version too old, requires 7.3 or later.". Don't get fooled by this.

Now try,


anooj.n@dv-mac-anooj-n ~$ sudo env ARCHFLAGS="-arch i386" gem install ruby-pg -- --with-pg-config=/Library/PostgreSQL/8.4/bin/pg_config
Password:
Building native extensions. This could take a while...
Successfully installed ruby-pg-0.7.9.2008.01.28
1 gem installed
Installing ri documentation for ruby-pg-0.7.9.2008.01.28...
Installing RDoc documentation for ruby-pg-0.7.9.2008.01.28...

PEACE. This worked for me.. :) Hopefully this should work for everyone.

Fixing the preverifier build error for Mpowerplayer on Mac OS X

If you’re using the Mpowerlayer SDK for your J2ME development on Mac OS X, and you get the following ant build error:

$ ant
Buildfile: build.xml

compile:

preverify:
[exec] Error preverifying class java.lang.Class
[exec] VERIFIER ERROR java/lang/Class.newInstance0()Ljava/lang/Object;:
[exec] Illegal type in constant pool
[exec] Result: 1

package:

BUILD SUCCESSFUL
Total time: 1 second

Then you can fix it by changing the following entry in your build.xml:

<property name="java.classes.jar" value="/System/Library/Frameworks/JavaVM.framework/Classes/classes.jar" />

to

<property name="java.classes.jar" value="/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Classes/classes.jar" />

This was the solution which i got after researching for quite sometime. However, this might have fixed issue for some, i was still facing the same issue.. :(

So after debugging for some more time, realized that
/System/Library/Frameworks/JavaVM.framework/Versions/1.4.2 was a symlink to the current jdk

dv-mac-anooj-n:Versions anooj$ ls -l
total 64
lrwxr-xr-x 1 root wheel 5 Jun 26 21:09 1.3 -> 1.3.1
drwxr-xr-x 3 root wheel 102 Jul 21 2009 1.3.1
lrwxr-xr-x 1 root wheel 10 Jun 26 21:09 1.4 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Jun 26 21:09 1.4.2 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Jun 26 21:09 1.5 -> CurrentJDK
lrwxr-xr-x 1 root wheel 10 Jun 26 21:09 1.5.0 -> CurrentJDK
lrwxr-xr-x 1 root wheel 5 Jun 26 21:09 1.6 -> 1.6.0
drwxr-xr-x 8 root wheel 272 Jun 26 21:11 1.6.0
drwxr-xr-x 9 root wheel 306 Jun 26 21:11 A
lrwxr-xr-x 1 root wheel 1 Jun 26 21:09 Current -> A
lrwxr-xr-x 1 root wheel 3 Jun 26 21:09 CurrentJDK -> 1.6

So now i just had to remove the symlink :

sudo rm 1.4.2

and paste 1.4.2 version folder here.. Issue resolved.

PS: i got 1.4.2 from my colleagues lappy.. Donno if apple allows you to download it.

Initialize List the way Arrays are initialized

If you want a fixed size list, wherein you don’t want to add/remove elements into it then probably this trick will save some line of code:

Consider this:

List<Character> vowels = new ArrayList<Character>();
vowels.add('a');
vowels.add('e');
vowels.add('i');
vowels.add('o');
vowels.add('u');


This could have also be written as:


 List<Character> vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');

Trying to add/remove elements from this list will result in an UnsupportedOperationException exception

Javascript refactoring

Might sound funny.. but ya.. i liked what i did today, so thought of sharing it..

So there was this combo box which needed to be pre-filled with currencies. This list of currencies was also loaded in a javascript array for other reasons.

The immediate refactoring was to use array to create options for the combo box. So i created a function which was called on body OnLoad event which would fill the select html element.

Was this required? This function will be called only once that too only onload of the page. Hmmm.. So my next refactoring was to remove the function and make javascript inline with my html body content.. Solved!

The advantages:

  • The function would make no sense, as in imagine a developer checking this code, he would be confused as to where all this function is used.
  • Debugging is easier. You don’t have to go to head to check your function, rather can just  write the js code where ur html element is.

The disadvantages(as mentioned by Kirit Pamar):

  • The javascript code is sent to the client each time a request is made to that page. This would be the case even if we called a method on body load. You could shove this script in a .js file and call the method on body load. This would improve the page load for each subsequent page request.

OpenX installation issue with postgres

OpenX official’s site says that it supports both Postgres as well as Mysql databases. But when i tried to install the OpenX, it only gave Mysql as the database option. Google’ing didn’t help much..
Banging head made me check the phpinfo() to check if my local php supports postgres. Luckily it was bang on.. It was not supporting Postgresql.

Fix:
Downloaded the latest installer for Mac OS X from here. Had to uncomment php module in the httpd.conf file located at /etc/apache2/httpd.conf so that php could be installed..

Get size of a required Schema in postgreSQL

Well, so my requirement this time was to get the size of all tables in a particular schema. HOW?

AFAIK, there was no direct way to fulfill my requirement. So wrote my own query..

SELECT pg_size_pretty(sum(pg_total_relation_size(‘archival.’||table_name))::bigint)
FROM information_schema.tables
WHERE table_schema=’archival’;

Where ‘archival’ is the schema name. Checkout the type-casting.
Short explanation: sum() returns int type, but pg_size_pretty() takes bigint as the param data type. Hence the type casting.

NB:- when copying the query, take care of the quotes.

Getting postgres table and database size

Getting Database size:
SELECT pg_database_size(‘user’);
pg_database_size
——————
58241934

Oh.. thats in Bytes.. Make it nicer.. Get db size in MBs.

SELECT pg_size_pretty(pg_database_size(‘user_table’));
pg_size_pretty
—————-
60 MB

Getting Table size:

SELECT pg_size_pretty(pg_total_relation_size(‘user_table’));
pg_size_pretty
—————-
55 MB

Setting PATH environment variable in MAC

This probably most of the MAC users must be knowing, but still would like to document it.:

I had a pain myself figuring the way out. Following is the way(or should i say PATH??) to do it:

  • In your terminal, type cd /etc/paths.d/
  • touch mysql(this could be any name)
  • sudo vi mysql
  • [press i to insert] type in the path u want to append to the $PATH variable. In my case i wanted to set mysql’s path. So i had to type(copy paste rather) /usr/local/mysql/bin
  • [esc] :wq! to save the file.

U are done..

There is another way to set up $PATH, which is rather simple but wont be persisted with new sessions(new terminal or terminal tab).

To do so, in terminal u can just say export PATH=$PATH:<the path u want to append>

for ex., PATH=$PATH:/usr/local/mysql/bin

TextMate Macro recording

Had to perform a series of regular expression search and replace actions. And had to do this on multiple files.. After doing it manually for sometime, got bored.. Cursed myself for violating DRY principle!

Well, to automate it, i used the TextMate’s bundled recording macro. Easy to use. Just start recording(Bundles->Macros->Start Recording). You can now replay it on multiple files.. Did my work.. Happy :)

Wow.. It’s a Limo…

We reached Dallas(DFW) airport at around 5.30pm.. Hell, they even charge for the trolleys here; that to $3.. We got one trolley, tried making the most efficient use of it.. 6 bags on one trolley… :)
Dad had enabled international roaming on his SIM.. So called up sis.. She said she wud be there within 10mins, as there was traffic on the highway..
So while waiting at the exit, i was privileged to see some amazing cars.. Cars that make every Indian open their mouth wide OPEN.. Mazda, Nissan, Porsche, Ford, Toyota, Honda, Audi being some brands.. Saw the amazing Ford MUSTANG :-)
After waiting for 15mins.. A white Corolla arrives.. It does not come close to us but is parked some 50metres away..
Suddenly sis comes out of nowhere!! She runs towards Dad.. Was expecting her to come and hug mom first.. But no..
“Hmmm… why did she hug dad first?? Oh.. She must have seen dad fuming with anger for coming in late.. Damn.. Munnabhai was correct.. Jadoo Ki Jhappi always to the rescue.. ” i said to myself after some brain processing.
She then hugs me and mom together..
I can see other 4 sis’ friends. After shaking hands i realise that the Corolla won’t be enough for us..
“Wats wrong with her?? She knew we wud be getting so much luggage.. Still she had to get her friends! Idiot.. Moron” speaking to myself.. Am sure mom and dad were with me on this though..
While sis’ friends are loading the Corolla, i see a white Limo behind..
“WOW.. Its a Limo… MOM.. see that.. Its a Limo..” i proclaimed..
“WAT?” mom replies as she is trying to count the bags being loaded in the car.. (Typical Mom)
“Limo mom… Limousine… In short LIMO” i reply excitedly!!
“It’s must be for some Big Person.. Its nice but” she replied..
I see sis moving away with remaining bags.. Wondering where she is taking it.. There were no cars behind that limo.. I can see a well dressed chauffeur loading the Limo with bags.. After sometime i realise that they were our bags that were making it into the Limo’s boot.
“WTF!!!!??? FUCK… WATS GOING ON HERE??????” i screamed!!!!
“Guys.. this is for you’ll… ” sis replies.. I am stunned!!!
“Are you kidding me?? Are u crazy? Are u in senses??” Well, i realize that they all mean the same.. But was out of words that time..
“Go on guys.. Its for you’ll..” she smiles.. and replies..

After sometime i find myself in one of the most luxurious car in the world.. WOW!!

Me and mom posing with the limo
Me and mom posing with the limo

Family re-union... in the US.. inside the Limo.. :)
Family re-union… in the US.. inside the Limo.. :)

Follow

Get every new post delivered to your Inbox.