Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04

Setting up Postgres with Ruby on Rails on Ubuntu 20.04

Postgres is one of the most powerful databases that is widespread among Ruby on Rails developers. There are many advantages to using this database, for example, easier deploying to Heroku.

In this tutorial, I will show you how to install Postgres, create a role and link the database with the Ruby on Rails application.

Let’s get started!

Step 1. Installing Postgres

sudo apt-get update
sudo apt install postgresql postgresql-contrib libpq-dev

postgresql is a general database package.

postgresql-contrib contains additional utilities and functionality.

libpq-dev use for compiling C programs communicate with Postgres.

Step 2. Creating a user

The installation procedure created a user account called postgres that is associated with the default Postgres role.

Let’s switch to postgres account to access to psql — terminal-based Postgres front-end program.

sudo -i -u postgres
psql

Now there is postgres=# environment. To create a new role enter the following command.

CREATE ROLE <your_username> LOGIN SUPERUSER PASSWORD '<your_password>';
# CREATE ROLE

Superuser bypasses all permission checks.

To list all users.

\du

Exit from psql and postgres environment using the command below.

exit

Step 3. Creating a Rails App

rails new app -d postgresql
cd app
bundle

To store the secrets I will use credentials.

EDITOR=nano rails credentials:edit

Add database username and password.

database:
username: <your_username>
password: <your_password>

Ctrl + X , Y and Enter .

Update the database.yml

https://medium.com/media/fd972cc1d6ab4274e7278b05b95a0da8/href

Create and migrate the database

rails db:create db:migrate

And start the server.

rails s

Bonus: Switch from SQLite to Postgres

Sometimes, you need to switch from one database to another. To do this follow these simple steps.

Update Gemfile

Delete SQLite gem from Gemfile.

gem 'sqlite3'

And add Postgres gem.

gem 'pg'

Run bundle.

bundle

Setup credentials and replace database.yml follow to instructions above.

If you have migrations files replace columns with t.string to t.text .

The last step is to create a new database.

rails db:setup db:migrate

Hooray! 🎉 Hope the tutorial helped you to set up the Postgres with Rails. Good luck.


Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Karim Marabet

Setting up Postgres with Ruby on Rails on Ubuntu 20.04

Postgres is one of the most powerful databases that is widespread among Ruby on Rails developers. There are many advantages to using this database, for example, easier deploying to Heroku.

In this tutorial, I will show you how to install Postgres, create a role and link the database with the Ruby on Rails application.

Let’s get started!

Step 1. Installing Postgres

sudo apt-get update
sudo apt install postgresql postgresql-contrib libpq-dev

postgresql is a general database package.

postgresql-contrib contains additional utilities and functionality.

libpq-dev use for compiling C programs communicate with Postgres.

Step 2. Creating a user

The installation procedure created a user account called postgres that is associated with the default Postgres role.

Let’s switch to postgres account to access to psql — terminal-based Postgres front-end program.

sudo -i -u postgres
psql

Now there is postgres=# environment. To create a new role enter the following command.

CREATE ROLE <your_username> LOGIN SUPERUSER PASSWORD '<your_password>';
# CREATE ROLE

Superuser bypasses all permission checks.

To list all users.

\du

Exit from psql and postgres environment using the command below.

exit

Step 3. Creating a Rails App

rails new app -d postgresql
cd app
bundle

To store the secrets I will use credentials.

EDITOR=nano rails credentials:edit

Add database username and password.

database:
username: <your_username>
password: <your_password>

Ctrl + X , Y and Enter .

Update the database.yml

Create and migrate the database

rails db:create db:migrate

And start the server.

rails s

Bonus: Switch from SQLite to Postgres

Sometimes, you need to switch from one database to another. To do this follow these simple steps.

Update Gemfile

Delete SQLite gem from Gemfile.

gem 'sqlite3'

And add Postgres gem.

gem 'pg'

Run bundle.

bundle

Setup credentials and replace database.yml follow to instructions above.

If you have migrations files replace columns with t.string to t.text .

The last step is to create a new database.

rails db:setup db:migrate

Hooray! 🎉 Hope the tutorial helped you to set up the Postgres with Rails. Good luck.


Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Karim Marabet


Print Share Comment Cite Upload Translate Updates
APA

Karim Marabet | Sciencx (2022-01-20T16:43:25+00:00) Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04. Retrieved from https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/

MLA
" » Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04." Karim Marabet | Sciencx - Thursday January 20, 2022, https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/
HARVARD
Karim Marabet | Sciencx Thursday January 20, 2022 » Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04., viewed ,<https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/>
VANCOUVER
Karim Marabet | Sciencx - » Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/
CHICAGO
" » Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04." Karim Marabet | Sciencx - Accessed . https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/
IEEE
" » Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04." Karim Marabet | Sciencx [Online]. Available: https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/. [Accessed: ]
rf:citation
» Setting up Postgres with Ruby on Rails 7 on Ubuntu 20.04 | Karim Marabet | Sciencx | https://www.scien.cx/2022/01/20/setting-up-postgres-with-ruby-on-rails-7-on-ubuntu-20-04/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.