Use PostgreSQL instead of SQLite (ruby on rails)
By: Jessica Michelle Farías Rosado
Tags: Postgresql, Beginners Guide, Ruby on Rails, Heroku, Sqlite

Update 16/01/2021

In this article you will learn about :

How to create a new project on Postgres automatically

How to change and an existing project to use Postgres instead of SQLite database.

How to deploy to Heroku after migrating to PostgreSQL

How to rename a project stored in Heroku

I recommend careful follow step by step in order to fix this list of common errors:

PG::ConnectionBad

FATAL: role “MyName” does not exist ActiveRecord::NoDatabaseError FATAL:database “db_name_development” does not exist” ActiveRecord::PendingMigrationError Migrations are pending. To solve this issue, run : db:migrate RAILS_ENV=development No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

Content:

How to create a new empty project using Postgres at the beginning automatically with one command line (ignoring default SQLite) How to change the database of an existing project with use SQLite into a PostgreSQL base project. (command line) How to deploy to Heroku through the terminal.

1. How to create a new empty project using Postgres at the beginning automatically with one command line (ignoring default SQLite)

The first thing you need to consider is to have the last version of Postgres, you can find the different versions here: https://www.postgresql.org/download

Ubuntu includes PostgreSQL by default. To install PostgreSQL on Ubuntu, use the apt-get (or other apt-driving) command:

Note: If you can install that line it’s throwing an error 404 please make sure to run what terminal require: sudo apt-get update

sudo apt-get install postgresql-12

So far you have installed PostgreSQL, if you run rails s, you going to see the next error:

PG::ConnectionBad

FATAL: role “MyName” does not exist

After that, you need to create a new username in Postgres as a local database, here we are going to create a new username “MyName”

sudo -u postgres createuser --superuser MyName

We usually create by default a project that uses SQLite database if you need to create a new project using just PostgreSQL. You can automatically create the project configuring the gemfile with all necessary gems and all related files that refer to the database as a PostgreSQL database:

COMMAND LINE:

rails new name_app --database=postgresql

If you run “rails s” or “rails server” you will have the next error in localhost:3000

ActiveRecord::NoDatabaseError FATAL: database “name_app_development” does not exist.

In order to fix this issue just need to create a new and empty database.

rake db:create

Try again “rails s” in your console and go to http://localhost:3000. ¿Did you get another error, just keep reading…

Now we have an empty database, if you already generate some models you should migrate all tables, so just migrate to create shema file inside “db” folder.

You should run the code above every time you created a new model, controller or table, or you going to have the next error message

ActiveRecord::PendingMigrationError Migrations are pending. To solve this issue, run : db:migrate RAILS_ENV=development

So in order to fix the issue that I wrote above, run in the terminal in root path:

bin/rake db:migrate

2. How to change the database of an existing project with use SQLite into a PostgreSQL base project. (one command line)

If you follow the next instructions you will be able to solve this next list of errors:

No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

If this is your first time trying to change the database you need to install postgress.

The first thing you need to consider is to have the last version of Postgres, you can find the different versions here: https://www.postgresql.org/download

Ubuntu includes PostgreSQL by default. To install PostgreSQL on Ubuntu, use the apt-get (or other apt-driving) command:

Note: If you can install that line it’s throwing an error 404 please make sure to run what terminal require: sudo apt-get update

sudo apt-get install postgresql-12

Note: If you can install that line it’s throwing an error 404 please make sure to run what terminal require: sudo apt-get updatePG::ConnectionBad

FATAL: role “MyName” does not exist

After that, you need to create a new username in Postgres as local database, here we are going to create a new username “MyName”

sudo -u postgres createuser --superuser MyName

Now it’s time to change the database. Rails 6.0 now has an extension to automatically change all Databases and install all gems that you need using just one command line.

COMMAND LINE:

rails db:system:change --to:postgresql

If you want to know more about this line of code go to https://weblog.rubyonrails.org/2019/8/15/Rails-6-0-final-release/

If you are trying to change an existing database you should delete the first database located on your computer whit the next command:

rm -f db/*.sqlite3

Now we just need to create a new and empty database

rake db:create

Now we have an empty database, it is time to migrate all tables already created in the model, so just migrate to create shema file inside “db” folder.

rake db:migrate

3. How to deploy to Heroku

(should have a PostgreSQL database first, read section 1 or 2)

Heroku is the easiest way to upload your project to the cloud.

Sign in or create a new account here: https://id.heroku.com/login

Make sure you have to install Heroku on your computer if this is your very first time that you are trying to deploy to Heroku run in your terminal:

Ubuntu:

sudo snap install — classic heroku

macOs:

bre tap heroku/brew && brew install heroku

If you already have Heroku install, now you need to login and create a new Heroku project, update the Heroku database, following the next simples steps:

Login to your account in Heroku through the terminal:

heroku login

Create a new project located in the cloud (you can not have more than 5 projects if you want to deploy your app free) go to the dashboard to make sure you don’t have more than 5 projects, then run:

heroku create

Adding all content of your project like(.)

git add .

and write a commit

git commit -m ”my_commit”

push all the main branch in Heroku, it must be the main branch, and use “Heroku” instead of “origin”, because if you are trying to upload your project to Heroku no to GitHub, make sure you are on the main branch.

git push heroku main

If you want to push another branch like the “feature” branch

git push heroku feature:main

For more info click here: devcenter.heroku.com/articles/duplicate-build-version

Update the database on Heroku, you can use Heroku as a check hero up terminal just write “Heroku run”:

heroku run rake db:migrate

If you want to change the name of your database first check the name of all your apps deployed on Heroku

heroku apps

Now just rename your application

heroku apps:rename newname --app oldname

If you want to change Heroku remote URL then:

Verify you currently remote URL with

git remote -v

Remove Heroku remote URL

git remote rm heroku

Create a new Heroku app again with

heroku create

If instead create a new app you want to set an existing app call “existing-app”

heroku git:remote -a existing-app

Follow me:

Show your support:

Twitter: https://twitter.com/FariasRosado

Github: https://github.com/jessicafarias

LinkedIn: https://www.linkedin.com/in/jessica-michelle-farias-rosado/

Happy coding! :D