This article will be using asdf
to install postgres and then refer to the article Ruby on Rails: Switch From sqlite3 to Postgres by Brenden Thornton to switch from sqlite3 to Postgres in Ruby on Rails.
Install required libraries in Ubuntu
sudo apt-get install build-essential libssl-dev libreadline-dev zlib1g-dev libcurl4-openssl-dev uuid-dev
If you have not installed asdf
, kindly checkout Install asdf (ruby, nodejs and yarn) in WSL2 to install asdf
, else just follow the following steps:
Add postgres plugin
$ asdf plugin add postgres
Install postgres
asdf install postgres latest
- Then, check the version installed (the versions mentioned here is the latest version I downloaded)
$ asdf list
postgres
14.2
- Add to your shell
asdf shell postgres 14.2
- Add to global
asdf global postgres 14.2
Start postgres
pg_ctl start
The rest refer Ruby on Rails: Switch From sqlite3 to Postgres from Update Gemfile to the end of the article.
NOTE:
- For replacing
sqlite3
topostgres
indatabase.yml
configuration, remember to switch the database and username to your database name and your username (check the code below with[]
). You can checkout GitHub on the configuration ofdatabase.yml
. Note that the [database_name] should be different for different environment.
development:
<<: *default
database: [database_name_dev]
test:
<<: *default
database: [database_name_test]
production:
<<: *default
database: [database_name_prod]
- I use
rake db:create:all
instead of
rake db:setup
to create database since I also want to create production database. By default, rake db:setup
will not create production database even though we have mentioned production
in the database.yml
. Checkout Stackoverflow to understand the differences of some of the commands.
-
rake db:create
creates the database for that particular environment (either is development, test or production) butrake db:create:all
creates the databases for all these 3 environments. - Remember to separate development database, test database and production database with different names!
- Check out "Run" and "Stop" to run and stop
postgres
in asdf-postgres documentation
Cheers~ Happy Coding!
Top comments (0)