This intro is written from a novice to a novice. I am no Unix expert, Mac expert, or expert in anything for that matter. If you’re fairly new to Mac/Unix, Ruby and Rails, this might help you as it’s written without the assumption you know much. If you are knowledgeable, this walkthrough will frustrate you.
Intro
If you are a Ruby/Rails programmer and have found it easy to work with Heroku to quickly deploy your apps (without having to learn the ins and outs of servers and Capistrano). When I started working with Heroku, I was surprised how easy it is to get your first apps running as compared to other servers, and free (to start with, at least). However, after a short time I ran into some trouble because I was working with MySql on my own machine, while Heroku insists on Postresql, which is stricter but is claimed to be faster and more professional.
To avoid running into deployment issues because of MySql/Postgres differences, it’s better to simply install the Postgresql Database on your own machine, in addition to (or even instead of) your MySql DB. This will ensure that most of what runs on your own computer will also work once you write that “git push heroku” line…
Another thing: when issuing commands like “sudo gem install” etc, the terminal asks for a password. This is would be your Mac’s password.
Credit
All the information on this tutorial is a concoction of other online tutorials. The most useful one I found was Greg Benedict’s guide
Full credit should go to Him. This is only a dumbed down version of it.
Different ways to install
There are a few ways of installing Postgres, two of them are: “making” them on your own computer (downloading the source code and compiling it), or – using MacPorts. What’s MacPorts? Mac Ports makes installations on your Mac (i.e. “porting it” to your Mac) a bit easier (please correct me if I’m wrong, I think it used to be called Darwin Ports. I’m happy it’s changed its name if so. I wouldn’t want a system as obsolete as Darwinism). Instead of you having to do a lot of hard work and compilations, it does it for you – most of it, at least.
Take note: Before you install MacPorts you need to have XCode installed on your computer. It comes with your Mac’s operating system installation CD, under “Optional” or “Other”.
Now you need to go to the command line on your Terminal (find Terminal under Applications =>
Utilities, and best to drag it to your Dock because you’ll use it a lot).
On your comnand line, type the following:
sudo port install postgresql83 postgresql83-server
This will install the Postgresql 8.3 DB on your computer.
You might get warnings about using the sudo command. Prefixing commands with “sudo” (meaning Super User DO) will enable you to do some “dangerous” things on your computer. It’s basically telling the computer “Look, I know what I‘m doing. Just do it, 0K?”.
Another error you might get is that it doesn‘t know what the command ‘port’ is. It might
tell you something like:
sudo: port: command not Found
That’s probably because in your “shell” (the terminal window), it still doesn’t recognise the path to MacPorts. All you have to do then is exit that window. Just close it, or type “exit” and then close it. Then open a terminal window again and it should work, so try it again:
sudo port install postgresql83 postgresql83-server
This should start the postgresql installation. On a new laptop it took about one hour to install, so you might like to do something like surf the internet mean while (but don’t do heavy surfing, like videos etc.), or just watch it work away. Mind you, I installed it on a WiFi connection in a busy caffe’, so it might be much quicker for you. It should be spitting out things to the screen until it’s finished.
Setup first DB
If you run the following commands in sequence, copy-pasting them one after the other, it will create a directory with a default database in it, create the necessary permissions for controlling it and initialise that database. Note there’s 3 lines here, the last of which is a long line.
Note that it creates ownerships of the file with a username called postgres and a password of postgres as well. This is the expected behaviour with postgresql.
It is advised that if you’re going to use postgresql a lot or as your default database, have it started up automatically each time you start up your computer. Having to start it up manually each time is a bother, brother. Do it like this:
If you wrote this command and got no error, you can restart your computer to get postgres working (bookmark this page so you can get back to it). Alternatively, you can write this command to start it up manually:
sudo su postgres -c ‘/opt/local/lib/postgresql83/bin/postgres -D /opt/local/var/db/postgresql83/defaultdb’
Notice, it’s one line. Also, if for any reason you don’t want it auto-started when you restart your computer, just start it up manually each time you restart your computer by using the same command.
Did you restart your computer? OK, I’m waiting.
Hi again.
If you want to check whether it’s working on your computer now, try writing this in the terminal:
ps -ax
You’ll see many lines. Scrolling up and down the lines, if you see lines with postgres: … etc, then it’s started up. We’re on our way.
Adding Postgres to the PATH
“Thy word [is] a lamp unto my feet, and a light unto my PATH.” (Psalms 119:105).
What’s the PATH? You might know what it is if you’ve used DOS. It’s when you write a command in the command line and the computer says “huh?”. It can’t find the command you wrote because it’s not in the “scope” of directories it’s looking at to find it. Sometimes you’ve got to manually add a path. It’s not so hard.
There are several files where you usually store PATH information. They’re usually called ‘profiles’. Depending on whether you want the PATH to be set for the current user or for all users, you would open different profile files and edit them with a text editor (I use TextMate).
If you use TextMate also, to edit the file quickly you can probably do the following:
sudo mate /etc/profile
If not, find a way of opening with a text editor. If there is a line with PATH in it, change it to add the path to postgres. If there isn’t one, add one. Here are some basic rules to doing this:
PATH=“/opt/local/bin:$PATH”
Look at the line. That’s they way it looks basically. For many, this line will be very long. For some, short. The format is:
PATH=DOUBLEQUOTES, DIRECTORIESSEPARATED BY COLON (:), CLOSEDOUBLEQUOTES.
One of the paths, or directories, is $PATH, which is to say, “Please recognise all these directories, plus any directories that you’ve already been instructed to recognise as PATH before.” (instead of just over-riding the existing PATH).
So what you need to do is add the path, or the directory of Postgres, to the list of paths it should recognise. That way, you’ll be able to fire up the command ‘psql’ without having to first look for the directory. Here’s a novice’s way to find the path to that directory:
Open Finder on your Mac’s “HD”, and search for the word ‘psql’ as the filename. That should find it. To limit results, look only in HD => opt directory, where postgres is most probably installed.
Here’s where MacPorts installed postgres on my own Mac:
/opt/local/lib/postgresql83/bin
You also add the paths to other important directories with executable files. Add “/opt/local/bin” and “/opt/local/sbin” to the list of paths so your path will end up looking like this:
Now, if you try to type ‘psql’ in the command line, it most probably won’t know what you’re talking about. You need to fire up a new terminal window. Close the present one if you like, and start a new terminal window. Then type ‘psql’, without the quote, of course.
Now, if you did that, it might have told you that:
psql: FATAL: database “WHATEVER” does not exist
That’s ok. At least psql is being found and called. We’ll take care of the DB problem in a second.
Creating your databases
To create your databases, you need to create a user for them:
substitute your_user_name with your Mac’s username.
After that, you can create your databases by issuing the command createdb with the name of the database you want:
createdb my_web_shop
Getting it to work with Ruby/Rails/ActiveRecord
We’ve got Postgres, we’ve got Ruby/Rails (you should have Rails if you’ve installed Snow Leopard). Now we’ve got to make them know how to work together and have peace in the Middle East. The following command will install a
For that we could write the following command (read on before you write it!):
sudo gem install postgres-pr
However, there’s a higher performance one that you could use. Try this instead:
sudo env ARCHFLAGS=“-arch x86_64” gem install pg
That’s basically it.
Administrating Postgres
There are different tools you can use to administrate postgres. I’ve tried Navicat under OS 10.4 (Tiger) and it crashed on me a lot. I don’t know why. In any case, the Navicat Lite version is for non-commercial use only, and although I make a pittance – still I consider what I do ‘commercial use’.
So now I’m trying PGAdmin and it has served me well so far.
Questions
If you’ve got any questions, I’m probably not the one to ask (you can try though…). Go to Greg Benedict’s tutorial and see the Q&A there.