The development and test sections are standard and Rails will handle everything. Pay attention to the production section. We'll be manually creating the database, the user and its password, so you'll have to use matching values.

You now should be able to have Rails setup the database.

bin/rails db:setup

Prepare your Ubuntu 16.04 server

Postgres is available on apt-get, so we'll use this to install it.

As usual, start by updating your packages and then installing the required packages

This will install Postgres and create a Linux user postgres with access. Since we are doing deploys and most of our management through the user deploy, we want to give access to this user too.

PostgreSQL manages database access permissions using the concept of roles. Let's switch to the postgres user to create a new role for our deploy user.

$ su - postgres
# if prompted for password, it is the same as your current user's
# now login into postgres and create a new role
$ psql
postgres=> CREATE ROLE deploy WITH CREATEDB CREATEROLE LOGIN PASSWORD 'passwordyouwanttouse';

Note that the role created has the same name as the Linux user we want to use, which is the username set in config/database.yml.

If we check all roles available with \du, we see the newly created role

Since we set password to be ENV['POSTGRES_BLOG_PASSWORD'] in our config/database.yml, the last thing we have to do is to add our database password as an env variable.

In Ubuntu, a good place to put environment variables that your want to be accessible from all shells (interactive or not, login or not) is /etc/environment. Let's add the following linePOSTGRES_BLOG_PASSWORD=passwordyouwanttouse to it.

Done!

You now have postgres installed on Ubuntu 16.04 and the deploy user with correct access to the blog_production database we manually created.

Your Rails app is setup to work with development and test databases, and knows to find the password in /etc/environment for the production one.

The good thing is that we didn't have to change anything to our deploy systems - I use Capistrano, and can simply run cap production deploy to deploy my new app and use Postgres.

Next

We now have postgres, but it's empty! Next post will be about migrating data from sqlite to postgres.