Installing Postgresql on Raspberry Pi

PostgreSQL is a popular database option used teams and companies of all sizes. From the smallest of hobbyist projects, to the largest of Enterprise online web applications.

In this tutorial, I'm going to explain how to install the latest Postgresql on the Raspberry Pi. I'm going to assume that you have Raspbian installed on your Raspberry Pi.

Installing the Essentials

In an open Terminal session window, type the following command to install PostgreSQL:

{% highlight ruby linenos %} sudo apt update sudo apt install postgresql {% endhighlight %}

Wait until the installation process is finished. Part of the installation process will involve the setup of PostgreSQL as a system service. Once the installation has finished, you can verify it's running via:

{% highlight ruby linenos %} sudo systemctl status postgresql {% endhighlight %}

You should see something like this:

{% highlight ruby linenos %} pi@raspberrypi:~ $ sudo systemctl status postgresql ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese Active: active (exited) since Sat 2019-09-21 15:53:47 MDT; 38min ago Process: 4031 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 4031 (code=exited, status=0/SUCCESS) CPU: 0 CGroup: /system.slice/postgresql.service

Sep 03 15:53:47 raspberrypi systemd[1]: Starting PostgreSQL RDBMS... Sep 03 15:53:47 raspberrypi systemd[1]: Started PostgreSQL RDBMS. {% endhighlight %}

It is really that simple; PostgreSQL has been installed!

Connect to the PostgreSQL console

In an open Terminal session window, use this command to connect to PostgreSQL the running console.

{% highlight ruby linenos %} sudo -i -u postgres {% endhighlight %}

Still connected to the console, you can run psql to help verify what version you're working with:

{% highlight ruby linenos %} postgres@raspberrypi:~$ psql psql (11.5 (Raspbian 11.5-1+deb10u1)) Type "help" for help.

postgres=# {% endhighlight %}

You can display a list of all the current databases.

{% highlight ruby linenos %} \l {% endhighlight %}

Since we haven't added one, you should see a list of 3 databases.

To quit the console, just type \q.

Enable Networking for PostgreSQL

In most cases, your PostgreSQL database will be accessed through a web application server running on the same machine - such as through PHP, Node, Ruby on Rails, etc. In which case, you don't have to do any further PostgreSQL configuration.

To allow your PostgreSQL databases to be accessed from the network via TCP/IP, you'll need to make a simple change to a configuration file. Open the following file with nano:

{% highlight ruby linenos %} nano /etc/postgresql/11/main/postgresql.conf {% endhighlight %}

Find the following line:

{% highlight ruby linenos %} listen_addresses = 'localhost' {% endhighlight %}

Update it to:

{% highlight ruby linenos %} listen_addresses='*' {% endhighlight %}

Close and save the file with ctrl+x. Finally, restart the running PostgreSQL service.

{% highlight ruby linenos %} sudo systemctl restart postgresql {% endhighlight %}

Done.

Conclusion

Hopefully this was a simple walkthrough to show you how to setup the latest version of the PostgreSQL database on your Raspberry Pi.

About the author

Hey, I'm Erik, a software engineer, gamer, writer, and content creator. I publish articles and tutorials about modern software development, design, and programming.

Get the newsletterBuy me a coffee

Wazoo