Just use Postgres

Leo Sjöberg • July 19, 2024

When you’re starting a new project, one of the first things you do is pick a database. The common choices tend to be between MySQL, Postgres, and MongoDB. Sometimes, you’ll see Sqlite, or some more niche databases like InfluxDB.

I’m here to tell you that, almost every time, you should just use Postgres.

The do-it-all database

Postgres isn't just a datastore. It comes with built-in functionalities that enables much more than relational storage. Need a queue? Use SELECT FOR UPDATE SKIP LOCKED. Want full-text search? Combine tsvector and tsquery with a GIN index. These features come out of the box, making Postgres a multi-faceted database solution.

In addition, Postgres supports complex datatypes like arrays and JSON, which means you can query almost any data structure in an efficient way when combined with a GIN index.

But what about MySQL?

MySQL has historically been a leader, largely down to its ease of installation on old web providers through cPanel. However, nowadays, if you're running a production environment, you should likely be using a managed database service from a major cloud provider. And at that point, the differences in maintenance become negligible.

And when it comes to performance, MySQL just doesn’t have much to offer over Postgres, while its feature set lags behind that of Postgres.

The unsung hero of Sqlite

Sqlite rarely gets much attention, but it can be a fantastic option for a really lightweight use case. It’s fantastic for local databases, such as when you're building a binary to distribute which needs a datastore. On a server, you’re probably better off with a managed database solution, but if you’re just spinning up small side projects, it can be a good and cheap alternative.

Handling document data

MongoDB is a popular option, particularly among software engineers working with JS/TS Node backends. However, in choosing a document database, you lose out on the ability to properly represent relations. And let’s face it, most data is relational. While there is often some need for storing data without a fixed structure (such as when you allow a user to configure a form with multiple elements), that’s often the edge case. And when you need to store those JSON blobs, Postgres offers the JSONB data type along with GIN indexing, which gives you great query performance and JSON operations, without having to run a separate document database.

Conclusion

At the end of the day, no matter what you’re doing (almost), Postgres has your back. Whether you need a robust relational database, JSON storage, queueing capabilities, or full-text search, Postgres has you covered.

So for your next project, just use Postgres.