DEV Community

Saral Karki
Saral Karki

Posted on

Rails with Postgresql

Postgresrails
During the last project, it was only when I was deploying the app to heroku that I learnt that heroku supported postgresql. I then changed the database to postgresql from the default sqlite that rails came with.

For the project that I am embarking on, I decided to start off using postgresql to begin with.

I created the app the usual way rails new (appname), and then proceded to change the database detail. In the database.yml file I changed the default details to

default: &default
  adapter: postgresql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production

I also added the gem pg to the gemfile and ran bundle install to install postgresql.

With this done, I ran rails db:create that created the database. I then created my users model. This time around I will be using the devise gem for the users authentication process. Finally, I ran rails db:migrate and my model was ready.

After all this was done, I found out that I could have skipped the process of changing the database.yml file had I mentioned the kind of database I would be using while generating my app with rails new app --database postgresql.

Did I learn something today? Yes, I did even though I did not get a lot of coding hours logged in today. I intend to start working with devise tomorrow, so I tomorrow's post will be all about getting started with the devise gem.

Top comments (0)