How to use PostgreSQL with your Ruby on Rails application on Ubuntu 16.04
Learn how to use PostgreSQL with your Ruby on Rails application, instead of the default SQLite database. SQLite is an easy-to-configure, lightweight product which ships with Ruby on Rails by default. However, PostgreSQL is a more robust solution which provides more advanced features, scaling, and stability, which may make it more suitable for your Ruby on Rails project.
Requirements
- A Cloud Server running Linux (Ubuntu 16.04)
- PostgreSQL installed and running.
- Ruby on Rails installed and running.
- A basic familiarity with Ruby on Rails.
All of the commands in this tutorial must be issued as the Rails user. This is the user account which you used to install and run Ruby on Rails.
Create a PostgreSQL user
Create a PostgreSQL user so that your Ruby on Rails application will be able to connect to the PostgreSQL database:
sudo -u postgres createuser -s [username]
This should be the same username which you used to install and run Ruby on Rails.
For example, to create the PostgreSQL username jdoe the command is:
sudo -u postgres createuser -s jdoe
To set a password for this user, log in to the PostgreSQL command line client:
sudo -u postgres psql
At the PostgreSQL prompt, enter the following command to set the password:
\password [username]
For example, to set the password for jdoe the command is:
\password jdoe
Enter and confirm the password. Then exit the PostgreSQL client:
\q
- Unlimited traffic
- Fast SSD NVMe storage
- Free Plesk Web Host Edition
Configure the Rails Application
The next step is to enable PostgreSQL support in your Ruby on Rails application.
Create the Application
First, create the application using the -d postgresql flag:
rails new [application name] -d postgresql
For example, the command to create an application named my-app is:
rails new my-app -d postgresql
The -d flag tells Ruby on Rails that you will be using PostgreSQL for this application.
Add the PostgreSQL username and password
Next, move into the directory which Ruby on Rails created for the application:
cd my-app
Edit the config/database.yml file:
nano config/database.yml
Scroll down to the section which reads:
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: my-app2
Delete the # in the last line to un-comment it, and change the username to the one you created:
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
username: jdoe
In the next section, delete the # to un-comment the last line, and add the password for the jdoe user:
# The password associated with the postgres role (username).
password: XPmMxZf
Save and exit the file.
Create the new application databases
Use the following rake command to create the databases for your application:
rake db:create
Test the configuration
To test the configuration, simply start the rails application and check it in a browser.
From the application's directory, use the command:
bin/rails s --binding=0.0.0.0
Binding the server to 0.0.0.0 allows you to view the application using your server's public IP address.
The server should respond with:
[user@localhost my-app]$ bin/rails server
=> Booting Puma
=> Rails 5.0.0.1 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.6.0 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://localhost:3000
Use Ctrl-C to stop
Switch to a browser and visit "http://your-ip-address:3000". For example, if your IP address is 198.162.0.1 you would go to http://198.162.0:3000.
If all is well and Rails is able to connect to PostgreSQL, you will see the default Rails welcome page.