Easily Set Up your Rails Application using a PostgreSQL Database

Ruby on Rails Tutorial— Part 1

CyberNotes
3 min readOct 28, 2022
  • One of the most annoying errors that I encountered while I was learning to create rails applications was how to properly set up my PostgreSQL database.
  • Rails is a Ruby framework that is used to build database — backed full stack applications for the web. As a result, we will always need a database when setting up rails.
  • By default, when you create a rails application, the database that is set up is the SQLITE database. Although you can always change that later, I always prefer setting up my database from the onset as I create my rails application

Lets look at how that’s done

1.0 Create your Rails App

  • In order to set up your rails application always remember to add the database flag as seen below when creating your new rails application:
  • Here, I will be creating a project called sample and integrating the postgreSQL database:

2.0 Configure the database.yml

  • Next, you’ll need to add the username and password to your config/database.yml
  • The thinking here is, once your rails application is created, it will access your database. However, your postgreSQL database is always set up with a username and password during installation.
  • This is the username and password that youll need to provide in the default section as seen below:
  • Here, I have provided a username and password for my database. If any of these details are incorrect, you will get an authentication error.
  • You dont have to do this for every section, as this is adapted by default using <<: *default. It is adopted in the development and test environment by default.
  • p/s: If you dont remember what your password or username was, you might be forced to uninstall and reinstall postgresql again unfortunately!

3.0 Create your schema.

  • The next logical step is to create the database.
  • If you look back in your config/database.yml file, there are databases that are outlined in the test and development environment. Since we are in development mode, we will create the database to handle the development and the testing bit.
  • Run the following command to create your database. This will create the sample_development and sample_test schemas as you see in your database.yml file. You can also change the names if you feel like.
Run the following command on your terminal:

If succesful, you should see your database created as seen below:

4.0 Finally, run your server.

Finally, run your server using the following command on your terminal:

If you land on the rails, welcome page, then you’re good to go!

Rails default welcome page!
  • Part 2 of the Tutorial can be found here

--

--

CyberNotes
CyberNotes

Written by CyberNotes

Data Science/Cyber - Student at Michigan State University.