Raspberry Pi

How to install PostgreSQL on Raspberry Pi

The Raspberry Pi OS is a Debian-based operating system that is used in all the devices of Raspberry Pi. PostgreSQL is a relational database that is based on an SQL language and stores the data in a sorted form. The PostgreSQL database stores the data according to the predefined schema and it also follows the JSON. It also has wide use in the development of mobile applications to store the application’s data.

The PostgreSQL can easily be installed on the Raspberry Pi just like in any other Debian-based distribution. In this write-up, PostgreSQL installation has been explained in detail.

How to install PostgreSQL on Raspberry Pi

The installation of the PostgreSQL on Raspberry Pi is easy, we will first update the repository of Raspberry Pi with the command:

$ sudo apt update

When the repository of raspberry Pi is fully upgraded and there is no need of any package to be upgraded, then we will execute the command to install the postgreSQL:

$ sudo apt install postgresql -y

The PostgreSQL has successfully been installed, but to authenticate the installation, we will check its status using the systemctl command:

$ sudo systemctl status postgresql

In the above output, we can see that not only the PostgreSQL has been installed but also is in “Active” status.

How to set up PostgreSQL on Raspberry Pi

To use the postgreSQL for storing data, we have to first set up the user of the PostgreSQL which we can do by executing the command:

$ sudo su postgres

As we have switched to the Postgres user, now we will configure the database in which we will first create a new user by using the command:

$ createuser pi -P --interactive

In the above command, we have created a user “pi” which is a default database user and used “-P” flag to set a password, “–interactive‘ is used to prompt more information about the user which is going to be created. As we execute the command, it will be asked for the password, set the password:

On entering the password, it will ask to enter the password again for the confirmation purpose, once the password is set successfully, it will ask three questions in which first, it will ask to make a new role as a superuser? We denied so typed “n”, second, to allow a new role to create databases? We allowed so typed “y”, and lastly, to allow a new role to create more new roles? We were allowed to type “y”. You can set all these preferences according to your requirements.

Once all this is configured, now, to run the PostgreSQL, use the command:

$ psql

We are in the PostgreSQL database, and to exit back the terminal of Raspberry Pi, use the following command twice times:

exit

Conclusion

The relational databases are among the popular databases used nowadays for storing data and managing data. The PostgreSQL database is also one of the relational databases which are used to store data and the best feature of PostgreSQL is a sophisticated locking mechanism that will restrict the users to modify the data of a database. In this write-up, we have discussed the installation procedure of PostgreSQL on Raspberry Pi as well as setting up by creating a new user in it.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.