Which Database Is Best For Raspberry Pi? (My Top 5)


Many projects on Raspberry Pi require a database system to store various data to use later. Hosting a database can even be a project in itself, but many options are available on Raspberry Pi, and making a choice isn’t always straightforward. In this article, I will introduce the most popular solutions, and explain the pros and cons in each case to help you decide.

As a whole, there is no perfect database management system that you should use all the time on Raspberry Pi, this is a personal choice. Each system has pros and cons, so the decision will vary depending on the project goals and the developer’s skills.

As a web developer, I tend to prefer to use the same technologies (programming language and database system). It’s not because they are the “best” choices, but rather because I’ll build everything way faster by using them. Anyway, if you don’t already have your favorites, let’s see which options you should consider.

By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only the essential concepts) required to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun parts. Get 10% off by downloading it today!

MariaDB (MySQL)

Download the Pi Glossary!
If you are lost in all these new words and abbreviations, request my free Raspberry Pi glossary here (PDF format)!

Even if many other options try to contest the leadership of MySQL in the database systems hierarchy, it’s still one of the most popular options. I did a poll on my YouTube channel, and almost 70% of my followers still recommend it on Raspberry Pi:

Introduction

MariaDB is a fork of MySQL, created when Oracle bought Sun (and so MySQL) a few years ago. It has improved a lot since then, so MariaDB is almost the default choice when installing a new MySQL server.

MariaDB is a great choice to store a large amount of data, especially if you need to manage several users with different sets of permissions. It’s particularly used for web projects (with the PHP language in general).

It works well on recent Raspberry Pi models with small to medium databases, but might be a bit too heavy for older ones or the Raspberry Pi Zero.

When you look at Google Trends, MySQL is still the most popular solution, so it will be easy to find documentation, forums, and help when using this database system in your projects.
MariaDB/MySQL uses SQL for queries, which is also the most popular language, so there’s no problem finding resources about it when you try to do complex queries.

As a web developer, I’ve mainly worked with MySQL and PHP for years, so it’s generally my default choice when starting new projects. I’m rarely disappointed or stuck with it, so I would recommend it in most situations.

Installation

MariaDB is available in the default repository on Raspberry Pi OS and most other distributions, so installing it should be straightforward. It’s one of the few solutions in this list that works on a client/server mode, which means you have to install a server first.

Download the Pi Glossary!
If you are lost in all these new words and abbreviations, request my free Raspberry Pi glossary here (PDF format)!

By the way, you can have a MySQL server on a different Raspberry Pi (or computer) if needed. If your project uses a very limited Raspberry Pi model but can access another device running the MySQL server, it’s fine.

Before installing the server, do the system updates with:
sudo apt update
sudo apt upgrade

Then install MariaDB with:
sudo apt install mariadb-server

But there are some extra steps after that to create the root password and the first user.
I explain everything in this full tutorial on how to use MariaDB on Raspberry Pi.

As MariaDB/MySQL can handle different levels of permissions, it can be a bit complicated for beginners, but you’ll quickly get it by reading my tutorial. I also share a graphical tool you can use to manage your MySQL server, without having to type tons of commands and SQL queries.

Usage

Once the server is installed, the MySQL server can be accessed with almost any language. You may need to install additional packages to use it, but you’ll easily find help for your favorite language.

For example, if you are using PHP to create your web project, you’ll have to install this package to connect to your new MySQL server with it:
sudo apt install php-mysql
Once done, you can use all the PHP functions listed on the PHP website.

In Python, you can use PIP to install the MariaDB connector, something like:
sudo pip3 install mariadb
There is a great tutorial on the official MariaDB website to get started with MariaDB and Python (link here).

CrowPi2
Learn Python, Scratch, & more in an easier way
This all-in-one kit comes with a Raspberry Pi 4-based laptop with tons of sensors and motors under the keyboard.

Buy now Check my review

Are you a bit lost in the Linux command line? Check this article first, for the most important commands to remember, and a free downloadable cheat sheet so you can have the commands at your fingertips.

SQLite

Introduction

SQLite is an interesting alternative to MySQL, especially on Raspberry Pi. This database system is way lighter (hence the name). You need less than 1 MB to install it, while MariaDB requires almost 200 MB of various packages.

SQLite is a server-less database system that is self-contained. It comes with many limitations that might be an issue for bigger projects (single user without authentication, for example), but for small projects at home, it’s often the easiest way to get started.

In short, SQLite is great for small and standalone projects that use a limited amount of data and don’t have any network connection.

Note: TinyDB is another alternative to consider, really close to SQLite. I won’t give a full introduction to it, but basically, it’s even lighter than SQLite. Some features are missing, but you shouldn’t need them most of the time for small Raspberry Pi projects.

Installation

To install SQLite on Raspberry Pi OS (and on any Debian-based distribution), you can simply run this command:
sudo apt install sqlite3

From there, you can use the sqlite3 command to create your databases and interact with them.
Here is for example my full log when testing in on my Raspberry Pi:

As you can see, I just tell the database file after the “sqlite3” command. It will create it if it doesn’t exist yet.

Then you can directly use SQL commands to create a table, insert data and do queries in it.
It can’t be more simple. Obviously, you need to know how to use the SQL language, but everything else is straightforward.

Usage

As for MariaDB, you’ll then need to install a connector to access the SQLite database from your project’s code:

  • With PHP:
    sudo apt install php-sqlite3
    Once installed, you can use the SQLite3 functions.
  • With Python, it should work directly, here is a basic example:

Remember that there is no security, users, and permissions with this solution. It’s great because it’s easy to set up and get started, but don’t use it for larger projects with sensible data.

By the way, if you get overwhelmed as soon as Python is required for a project, I recommend checking out my e-book “Master Python on Raspberry Pi“. It will guide you step-by-step to learn the essential concepts (and only the essential concepts) required to achieve any project in the future. Raspberry Pi without Python is like a car without an engine, you miss all the fun parts. Get 10% off by downloading it today!

MongoDB

Introduction

MongoDB is very different from the previous options listed in this article. It’s a non-relational database management system, meany the data storage is very flexible. Basically, it’s an association of keys and values, that can be from any type (strings, arrays, objects, etc.).

It doesn’t use the SQL language or any variant of it. It uses MQL (MongoDB Query Language) which is completely different from what you might be used to.

Overall, it’s a decent option when you have no experience with MySQL (some people will argue that it’s easier to start with MongoDB instead of MySQL) and need more flexibility (for example, if your project is not completely defined and may evolve a lot over time).

To be honest, I’m not a big fan of MongoDB. I had many issues with it, in terms of security and basic understanding of how to find what I’m looking for in databases created by other developers. But that’s probably because I know it less than SQL-based solutions.

Installation

Installing MongoDB on Raspberry Pi OS is particularly complex, as it’s not available in the default repository, and they don’t have any compiled package you can download from their website.
The only option is to compile it from sources, as explained in their documentation.

It took me a while and it finally crashed my system, so I can’t really recommend it, but if you want to give it a try, check their documentation. I also found a Mongo for Pi project on GitHub, but I didn’t test it, so I can’t really recommend it either.

The easiest way to get MongoDB on your Pi is to install Ubuntu first. Then you can follow this page on their documentation to add the corresponding repository and follow the other configuration steps.

Usage

Once MongoDB is installed and your first database is created, using it on Raspberry Pi is not very different from the previous solutions.

For example, to connect to your Mongo database with Python, you first need to install this library:
sudo apt install python3-pymongo
And you can then import “pymongo” in your script and use all these functions.

PostgreSQL

Introduction

And the last option I want to introduce in this list is PostgreSQL. It has been a fight with MySQL for a long time. It looks like MySQL/MariaDB won the game, but PostgreSQL is very stable in the trends, so I guess there are still a lot of developers using it:

The main difference with MySQL is that PostgreSQL is an object-oriented database, while MySQL is a relational database. PostgreSQL is also recommended for complex queries, large data warehouses, and data analysis in general.

It doesn’t exactly fit with the traditional Raspberry Pi usage, which is generally for small projects. But if you are used to PostgreSQL, I understand you might be interested in using it on your Raspberry Pi too.

Installation

Like MariaDB, PostgreSQL is available in the default repository on Raspberry Pi OS, so you can install it with:
sudo apt install postgresql

You’ll then need to create the first user with:
sudo su postgres
createuser $USER -P --interactive

And then use the psql command to connect to your database, and use the SQL language to manage everything:
psql

Usage

PostgreSQL is very popular and works with almost any language.
If you want to use it with Python, the most commonly used library is psycopg2:
sudo pip3 install psycopg2

You can then import it into your scripts, and use the class to connect to your database.
Here is a great tutorial to get started with Python and PostgreSQL.

Want to chat with other Raspberry Pi enthusiasts? Join the community, share your current projects and ask for help directly in the forums.

Related questions

Get My Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Can SQL server run on Raspberry Pi?

SQL Server is the database management system created by Microsoft for Windows servers. They released a version for Linux servers a while ago, but it’s only designed for x86 architectures, meaning it’s incompatible with a Raspberry Pi.

An alternative would be to run Azure SQL Edge in Docker or have another computer running Windows host the server. You can then use ODBC with most languages to connect to the SQL server from your Raspberry Pi.

Overall, Microsoft products (Windows or SQL server) can work but are not optimized for the Raspberry Pi, so I wouldn’t recommend using them for now.

How to view and manage databases on Raspberry Pi?

Each database management system has a set of tools you can use to make your life easier and manage the databases and permissions from a graphical interface. MariaDB/MySQL has PHPMyAdmin, SQLite has DB4S, MongoDB has Compass, etc.

So, even if you’ll need to type a few commands to install them, you’ll most likely use a GUI after that for the daily operations.

Raspberry Pi Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.

Need help building something with Python?
Create, understand and improve any Python script for your Raspberry Pi.
Learn the essentials, step-by-step, without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.

Patrick Fromaget

I'm the lead author and owner of RaspberryTips.com. My goal is to help you with your Raspberry Pi problems using detailed guides and tutorials. In real life, I'm a Linux system administrator with a web developer experience.

Recent Posts