Postgres (or PostgreSQL) is an open source database. Ruby on Rails is an open source web framework made in Ruby. Rails is database agnostic, this means you may use it with a variety of other databases. On default it will assume that MySQL is the one used, however it’s pretty simple to use with Postgres instead.
In this tutorial we’ll demonstrate how you can create a Rails application which uses a Postgres database. You may follow the tutorial on your local machine or a VPS.
Installing Rails using RVM
The simplest way to install Rails is with RVM, this will also install Ruby. In order to Install RVM you’ll have to make sure that your system has curl installed, (this changes depending on the type of OS). If you happen to have RVM installed, feel free to skip to the next part of the tutorial.
RVM may install Ruby and Rails automatically as part of its installation. To do so, execute the following command:
\curl -L https://get.rvm.io | bash -s stable --rails
Be sure to review the RVM install script before executing it, the same goes for any other remote script that you pipe into bash.
RVM is going to install itself on your system. Then, you’ll be able to use it to manage your Ruby versions. This is useful as you might need other versons of Ruby for various projects. RVM also installed the Rails gem for us.
Installing Rails using RubyGems
In case you already have RVM installed, you shouldn’t have to re-install it. Instead, you may just install Rails by installing the gem:
gem install rails
This is going to install Rails and every other gem it needs.
Please keep in mind that when installing Postgres it also depends on your OS. Look here for the whole list: postgresql.org/download.
Usually It’s the simplest to just use a package manager such as apt-get on Ubuntu or Homebrew on OS X.
In case you’re installing Postgres on a local machine you might also need to install a GUI (however this tutorial assumes command line usage). pgAdmin isn’t the best tool in the world, but it should do.
Finally, you’ll want to start installing the pg gem so you may interface with Postgres from Ruby code. To do this, execute the following:
gem install pg
Setting Up Postgres
In this step, we’ll create a Postgres user for the Rails app. To do this, change into the Postgres user by executing the following:
su – postgres
Now we’ll create a user (or a “role”, as Postgres says):
create role myapp with createdb login password ‘password1’;
Creating Your Rails App
To make a Rails app configured for Postgres, execute the following command:
rails new myapp --database=postgresql
This is going to create a directory named “myapp” this will contain an app called “myapp” (you may name it as anything you’d like when running the command). Rails will expect the name of the database user to match the name of the application, however you may change that if required.
Next, we’ll configure what database Rails will talk to. We can do this by using the database.yml file, located at:
Tip: RAILS_ROOT is the root directory for Rails, like we’ve shown in the example above, it will be at /myapp (relative to your current location).
The database.yml file is used by Rails to connect to the right database for the current Rails environment. It will use YAML, a data serialization standard. There’s multiple databases listed here for various environments, development, production, and test. On default Rails is going to assume a different database for each environment. This will prove to be quite useful because, for example, the test database will be emptied and rebuilt every time you run Rails tests. For every database, make sure that the username and password match the username and password you’ve provided your Postgres user.
After It’s configured, your database.yml will have something like this:
development: adapter: postgresql encoding: unicode database: myapp_development pool: 5 username: myapp password: password1 test: adapter: postgresql encoding: unicode database: myapp_test pool: 5 username: myapp password: password1
Now you can run:
This is going to make development and test databases, select their owners to the user specified, and create “schema_migrations” tables in each. The table’s job is to record your migrations to schemas and data.
You’ll now be able to start your Rails app:
Once you navigate to localhost:3000, a Rails landing page should present itself. However, this won’t really do much.
In order to interact with the database, we’ll create a scaffold:
rails g scaffold Post title:string body:text
Finally, go over to localhost:3000/posts and from there you may create new posts, delete posts and edit existing posts as you wish.
See the Rails getting started guide for more introductory operations.
Your Rails app should now be talking to a Postgres database, congratulations!