DEV Community

Abdusaid Abdurasulov
Abdusaid Abdurasulov

Posted on

Set up your Rails app with the PostgreSQL database on Ubuntu

This article assumes that you have Rails and PostgreSQL installed on your device.
To start, go to the folder where you want to create a new Rails app and run the terminal in that folder. Then using the following command create your app. Note that toyapp is the name of an app you can replace it with whatever the name you would like to call:
rails new toyapp -d=postgresql
Here the -d=postgresql option is for setting up the database with PostgreSQL.
If everything went well, go to the newly created directory:
cd toyapp
Now we need to configure and create our database. To do that we will first store the password of the PostgreSQL role in the environmental variable. So that we do not have to write the password in the configuration file, to keep it safe:
echo ‘export TOYAPP_DATABASE_PASSWORD=’’YOUR_PostgreSQL_Role_Password”’ >> ~/.bashrc

Then export the variable for your current session using the source command:
source ~/.bashrc
Now open the configuration file toyapp/config/database.yml in your favorite editor. In the default section, include the following lines at the bottom:

username: yourusername

save the file and run the following command to create the database of an application:
rails db:create
If you have correctly entered username and password for the PostgreSQL role you should get the output like this:
Alt Text
Now it’s time to test whether the configuration was right, run the built-in webserver in Rails by the following command:
rails server --binding-
When the server runs you should get this output:
Alt Text
Using your favorite browser go to the or localhost:3000
Alt Text
If you get the above result you successfully configured your Rails app to use PostgreSQL database.

Top comments (0)