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.
I recommend switching to PostgreSQL before creating your first production environment.
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.
To switch to PostgreSQL:
- Install PostgreSQL on your development machine and make sure that it is running.
- Change the database gem. In
gem 'pg'. Then run
bundleto update your gems.
- Change the database adapter. In
config/database.ymlreplace the adaptor:
- Change the database names in
projectname_developmentfor the development database,
projectname_testfor the test database, and
projectname_productionfor the production database.
Test that this works by running
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.