The PostgreSQL database management system, also known to many as Postgres, has many decades of development behind it. It originally started as a project at Berkeley University in California. Today, the open source database continues to defy solutions from commercial competitors, since the development team is constantly working on its functionality and performance. But what exactly is PostgreSQL?...
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.
- 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:
For example, to set the password for jdoe the command is:
Enter and confirm the password. Then exit the PostgreSQL client:
vServer (VPS) from IONOS
Low-cost, powerful VPS hosting for running your custom applications, with a personal assistant and 24/7 support.
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:
Edit the config/database.yml file:
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:
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 18.104.22.168 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