Since I’ve read so much about PostgreSQL being the other great open-source database server, I thought I’d try it out on a new Rails application I’m starting up. Right now, MySQL still seems to be the darling of the Rails development folks, so it’s a little more difficult to find information on installing and getting PostgreSQL working with Rails. So, here are some pointers I’ve collected to get PostgreSQL up and running.
Please note: These instructions were used to build PostgreSQL on my MacBook Pro (Core 2 Duo), so some of the below may not apply to anyone else. I am not responsible for any mess you get yourself into.
First, of course, you’ll need to download PostgreSQL, which you can do here: http://www.postgresql.org/ftp/source/ (Follow the link with the highest version number, and choose the postgresql-8.X.X.tar.gz file).
Next, you need to build the source. I have a ‘tmp’ directory just off my personal directory for just this sort of thing.
billturner:~/tmp bill$ tar zxvf postgresql-8.X.X.tar.gz
billturner:~/tmp/ bill$ cd postgresql-8.X.X/
If you’ve already run through Dan Benjamin’s wonderful instructions on getting Ruby, Rails, MySQL, etc. running on a Mac, then you’re already set with the necessary Readline libraries. If not, jump over to that tutorial and see how to get the readline libraries installed.
I’ll assume that you have readline installed in /usr/local like Dan suggests. If not, change the file paths below on the configure command.
billturner:~/tmp/postgresql-8.X.X bill$ ./configure \
> --with-includes=/usr/local/include \
> --with-libraries=/usr/local/lib
billturner:~/tmp/postgresql-8.X.X bill$ make
billturner:~/tmp/postgresql-8.X.X bill$ sudo make install
Here’s where I initially ran into problems. The postgres ruby gem just wouldn’t compile or install. After some searching online, I found a solution (this comment) that worked.
First, let’s attempt to install the gem:
billturner:~ bill$ sudo gem install postgres
And if you have a Mac similar to mine, you may get an error like the following:
Building native extensions. This could take a while...
postgres.c: In function 'pgconn_s_escape_bytea':
postgres.c:222: warning: pointer targets in passing argument 1 of 'PQescapeBytea' differ in signedness
It will actually say “Successfully installed postgres-0.X.X” at the end, but if you get that error, it wasn’t actually installed.
If you do not get this error, then hooray for you and you can skip the following instructions. If you do get that error, you’ll need to do some fixin’.
You’ll need to get into the directory where “gem” put the source.
billturner:~ bill$ cd /usr/local/lib/ruby/gems/1.8/gems/postgres-0.X.X
Now, run the configuration again, since the Makefile wasn’t created when running “gem install postgres”.
billturner:/usr/local/lib/ruby/gems/1.8/gems/postgres-0.X.X bill$ sudo ruby extconf.rb \
> --with-pgsql-include-dir=/usr/local/pgsql/include \
> --with-pgsql-lib-dir=/usr/local/pgsql/lib
Then you need to open up the file “postgres.c” in a text editor (most likely with the sudo command):
billturner:/usr/local/lib/ruby/gems/1.8/gems/postgres-0.X.X bill$ sudo mate postgres.c
Jump to line 222, and change it from this:
to = (char *)PQescapeBytea(from, from_len, &to_len);
to this:
to = (char *)PQescapeBytea((unsigned char *)from, from_len, &to_len);
Save that and we can now compile and install.
billturner:/usr/local/lib/ruby/gems/1.8/gems/postgres-0.X.X bill$ sudo make
billturner:/usr/local/lib/ruby/gems/1.8/gems/postgres-0.X.X bill$ sudo make install
If you take a look through this helpful tutorial on the Apple.com website, about a page and a half down, you’ll see a section with a screenshot explaining how to create a user for the PostgreSQL server. It mentions in that article that the user needs to be an “administrative user,” but I haven’t found that to be the case. I created a basic “postgres” user, and that’s it.
Now that you’ve created the system user for the database server (we’ll assume it’s “postgres” from here on out), let’s get the data directory set up for the initial data:
billturner:~ bill$ sudo mkdir /usr/local/pgsql/data
billturner:~ bill$ sudo chown postgres /usr/local/pgsql/data
Now, “su” into the postgres account to set things up the rest of the way with this initializing command:
billturner:~ bill$ su -l postgres
billturner:~ postgres$ /usr/local/pgsql/bin/initdb -E UTF-8 -D /usr/local/pgsql/data
Here, there’s a possibility you may get another error at this step. The first time I ran the initdb command I left off the “-E UTF-8” option and it ran without a problem. Since, I’ve decided to specify UTF-8 encoding. When I added the “-E UTF-8” option, I received an error similar to this:
FATAL: could not create shared memory segment: Cannot allocate memory
DETAIL: Failed system call was shmget(key=1, size=1081344, 03600).
HINT: This error usually means that PostgreSQL's request for a shared memory segment exceeded available memory or swap space. To reduce the request size (currently 1081344 bytes), reduce PostgreSQL's shared_buffers parameter (currently 50) and/or its max_connections parameter
(currently 10).
If you get this error, you’ll have to make a change to the system that requires a reboot. Thanks to some Google searches you’ll need to edit, the /etc/rc file (you’ll need to use “sudo”). Find a line that looks like this:
sysctl -w kern.sysv.shmmax=4194304 kern.sysv.shmmin=1 kern.sysv.shmmni=32 kern.sysv.shmseg=8 kern.sysv.shmall=1024
and change the value of “kern.sysv.shmmax” to 167772160 and “kern.sysv.shmall” to 65536. The line should now look like this:
sysctl -w kern.sysv.shmmax=167772160 kern.sysv.shmmin=1 kern.sysv.shmmni=32 kern.sysv.shmseg=8 kern.sysv.shmall=65536
Save the file and reboot. Once your Mac comes back, you’ll need to run the “su -l postgres” and the “initdb” commands above this error fix. The initdb command should complete without errors this time.
If you don’t feel comfortable making this change, just omit the “-E UTF-8” from the last command above, and you shouldn’t have any problems.
It appears that there are loads of different ways to start the PostgreSQL daemon. Here’s the one I went with:
billturner:~ bill$ su - postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start'
Wallah! PostgreSQL is now running. However, once you shut down or reboot your Mac, it won’t start up on its own. Yet.
I’m including a section below on how to have PostgreSQL start up automatically when rebooting, but until I get a little further into developing with this database, I’ll just be able to run it when I want it. So, in order to make starting and stopping the server easier, I added the following two lines to the bottom of my ~/.bash_login file:
alias pgstart="su - postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start'"
alias pgstop="su - postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop'"
Note that in each case, you’ll be prompted for the “postgres” user’s password. You can avoid something like this if you set up PostgreSQL to automatically launch at boot.
Here I’ve ran into so many, many different approaches, I’m not sure which one to highlight, so I’ll just tell you what I’ve done. Since MySQL can now install a StartupItem (that is handled by the launchd service on the Mac), I went to find an appropriate one for PostgreSQL.
As a base, I downloaded the StartupItem installer package from Marc Liyanage’s excellent resource. Here’s a direct link to the package (that may or may not work in the future). From my searching, this seemed to be the simplest, and closest approach to what I needed.
Once you install the StartupItem, it won’t work as installed. It puts the right files in the right place, but the command directives point to the wrong locations. So, with your favorite text editor open the StartupItem command file:
billturner:~ bill$ sudo vi /Library/StartupItems/PostgreSQL/PostgreSQL
Find the line that starts the server(in the StartService() section), which looks like this:
su - postgres -c '/usr/local/bin/pg_ctl start -D /usr/local/pgsql/data -l /usr/local/pgsql/logfile -o -i'
and change it to this:
su - postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start'
Next, find the line that stops the server (in the StopService() section), which looks like this:
/usr/local/bin/pg_ctl stop -D /usr/local/pgsql/data
and change it to this:
su - postgres -c '/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data stop'
If you’d like to try another approach, this search should be a decent starting point.
Whew! Okay, that’s a lot to take in, as I know it was a lot to write out. Hopefully these instructions have helped someone, that like me, was stuck at various steps in getting PostgreSQL installed on my Mac.
If you have some pointers or tips for me, I’d be glad to see them.
The Pantheon
Not a whole lot to see, but just the fact that it’s so old and still standing is a feat in itself.
Piazza Navona
This is just a short walk west of the Panteon, and while there isn’t a whole lot to see, it has 3 (I think) pretty ornate fountains, but it’s huge, and filled with artists and sometimes street performers. A nice place to sit and relax.
St. Peter’s/Sistine Chapel/Vatican Museum
The first two of these are definite must sees. If you’re religious or not, they’re both beautiful and awe-inspiring. There’s a lot to see, so either plan a huge chunk of the day to these, or when going down the looong, large hallways to get down to the Sistine Chapel, don’t stop and look at everything too closely. It’s all great art and tapestries and armory, but its a long walk down and back. And save a good chunk of time for St. Peter’s. It’s a HUGE church with amazing art & sculpture on every column and in every corner. If you have some spare time, and aren’t afraid of narrow spaces, try climbing to the top of the cupola (I think there is another name for this). If you go all the way up, you get a great view of Rome.
Trevi Fountain
A very large and impressive fountain
The Colosseum
I think it’s the fact that this is so old, and still relatively in one piece that makes this more impressive. Sadly, when we were there they had this runway-looking platform in the middle of the place that kind of detracted from the whole experience. Here’s my pic of it: http://brilliantcorners.org/gallery/display/7/8/
The Roman Forum
This is pretty close to the Colosseum, but we didn’t have time to check it out. It looked pretty neat though.
The Ponte Vecchio
This is the famous bridge over the river Arno. Mainly it’s just a bunch of silver and gold shops. Neat stuff to look at, some seems pretty cheap, but with the weak dollar, you may not be getting that great of a deal.
The Duomo
You can’t miss this one, since it’s basically in the center of town. When we were there it was pretty empty inside, but still neat.
Accademia
This is the museum where Michelangelo’s David is located. There are some other interesting pieces in here, but oh boy, you have to go just to see David at least. You may have to wait in a somewhat long line.
Uffizi
The other big art museum in Florence. Holds such masterpieces as Botticelli’s famous “The Birth of Venus” and “La Primavera”. This place is HUGE, so expect to walk a lot inside. But, it has many more interesting pieces than the Acaddemia in general.
Mercato Centrale
Just a great and huge indoor market, with shops selling everything from meats and cheeses to seafood and trinkets. If you’re hungry, some of the places will even put together a sandwich for you. Yum!
Other museums
The Barghello & Medici palace are all supposed to be great, but we didn’t get the chance to see them.
Boboli Gardens
Also supposed to be a beautiful place, but again, didn’t have the time.
Trattoria Angiolino
Via S. Spirito 36/R This was the only card I could find from a restaurant in Florence. It’s located on the other side of the river from the rest of the main city (our room was on this side of the river). But, it’s pretty small, great atmosphere, and the food, like just about everywhere else, was fantastic!
San Marco square
The big square you’ll recognize from commercials and movies. Not a lot to see, other than the milling around of other tourists.
Palazzo Ducale (Doge’s Palace)
This is situated in the square. It’s a pretty impressive palace with lots of great art and an amazing armoury.
The city itself
There are so many small and winding streets everywhere with hundreds of bridges over all the little (and big) canals. Just wandering around was fun here. Plenty to see and do without having to pay a thing.
Harry’s Bar
Close to the square, and supposedly one of Ernest Hemingway’s favorite places to go. Looked crowded and a little upscale when we looked in, so we didn’t go in.
It was closed the night we tried to go here, but we’ve heard from so many people how good it is.
Here’s a restaurant that was recommended to us by a local street artist when the Madonna was closed. This was the best meal we ate while in Italy. Rialto (Mercato del Pesce) S. Polo, 249 Tel #: 0415225953 My wife, brother-in-law, and I split the “grillo misto” (we think that’s it)(in addition to getting our own pasta dishes), which was a catch of the day grilled seafood sampler, many different kinds of fish. very delicious.
But above anything and everything else you’ll hear, just have a good time. Italy is a beautiful country. I was only there for 9 days, but already I know I want to retire there, if not move over there (somehow) to live and work before then. You’re gonna love it. :)