Installing PostgreSQL and the PostgreSQL Ruby Gem for Windows, Linux and Mac OS X

Yesterday was a bit of a challenging last day for the Ruby on Rails workshops because of the weather and a lot of factors but a lot came. Thank you! Of course, I have a lot more to say about that but I am a bit focused on solving some challenges right now.

One of the gotchas we encountered was the fact that we used SQLite for development and PostgreSQL for production. We use an ORM but then that does not mean everything will just work regardless of what relational database management system we would use.

Today I took some time to finally get back to learning Windows! But I am not encouraging the use of Windows for full time work if you want to be a full time Rubyist. In fact, if you are doing more backend development work, you should be using Linux or Mac OS X.

I would like to repeat what I said yesterday:

I strongly recommend that you use the same RDBMS for development, test and production.

This means that if you use PostgreSQL in production then you should use PostgreSQL in development. It’s not difficult to get started regardless of your operating system.

For Windows users, you have two options and it greatly depends on your development setup. If you are using XAMPP, we recommend downloading the zip archive of the binaries.

If you do not have XAMPP or any Apache for Windows installed, you have to download the PostgreSQL Windows installer. If you are using Windows 8 32-bit version then you have to download “Win x86-32.” Yes, downloading will take a while if you have the same crappy internet connection that I have. Because of Windows 8 security features, you will have to unblock the file downloaded before you can execute it. Just right click and click “unblock” and it should work.

You will be asked for a password. If you are not great in memory work, just use “postgres” or something else you can remember.

Do not change the port 5432. Just click “next” button.

It will ask a few more things needed so you have PostgreSQL admin working locally too. If you’re wondering what to select when you reach the StackBuilder part, choose “PostgreSQL 9.3 on port 5432”

Now you will have to choose to install Apache and phpPgAdmin. Those will be very helpful in the future.

For Mac users who have Homebrew, it is installing by executing a command:

  brew install postgresql

To find out more about other commands you should execute, please do:

  brew info postgresql

For Debian and Ubuntu users, it can installed by executing this command:

   sudo apt-get install postgresql-client

You will have to create a new user which may sensibly be the same as your current username for Ubuntu/ Debian.

 sudo -u postgres createuser --superuser $USER
 sudo -u postgres psql

For more information, visit the PostgreSQL documentation for your Linux distro.

Now that you have PostgreSQL and a user, you can probably install the PostgreSQL Ruby gem.

On your terminal or command prompt, execute this command for installing the gem:

gem install pg

To test if PostgreSQL is working with the Ruby gem, you will have to could a new Rails app with postresql as the database. That is sensibly done this way:

rails new blog -d postgresl
bundle

Before doing anything, you will have to update the config/database.yml file.

For those who installed on Windows with my suggestion of using “postgres” for username and “postgres” for password, all you have to do is to change the default username “blog” to “postgres” and the password to “postgres.”

For those who are using a Mac or Linux and have followed the suggestion of creating a user based on current user, you probably do not need to update the password.Just change the username to “katz” or the Linux username you are current using.

rake db:create:all

That command creates the databases. If you get no errors then it is working.

Sounds easy. Now let’s test by creating a blog application through Rails scaffold command:

rails g scaffold Post title:string body:text published_at:datetime
rake db:migrate

Start the server through rails s command and go http://localhost:3000/posts.

Hope this helped.