Switch to PostgreSQL
Published: Sep 22nd, 2020
This is part of Joyful Rails, a list of recommendations to make developing your Rails app more productive and joyful.
In this article, we are talking about switching to PostgreSQL.
When
I recommend switching to PostgreSQL before creating your first production environment.
Why
Rails uses SQLite as the database by default because it is fully-contained; there is no server to manage and the data is kept in a local file.
However, SQLite is not suitable for most production environments; PostgreSQL is a much better choice.
PostgreSQL should also be used in development in order to maintain dev/prod parity.
How
To switch to PostgreSQL:
- Install PostgreSQL on your development machine and make sure that it is running.
- Change the database gem. In
Gemfile, replacegem 'sqlite3'withgem 'pg'. Then runbundleto update your gems. - Change the database adapter. In
config/database.ymlreplace the adaptor:adapter: sqlite3withadapter: postgresql. - Change the database names in
config/database.yml. Useprojectname_developmentfor the development database,projectname_testfor the test database, andprojectname_productionfor the production database.
Test that this works by running rake db:create.
Alternatives
Large and successful projects are also run on MySQL. In the past, MySQL had some severe limitations but those issues may be papered over by Rails or fixed entirely.
I wouldn’t risk it, but others certainly do.
Talaria Software