A guide for installing, setting up, and using PostgreSQL on Linux (Ubuntu).
PostgreSQL is a Relational Database Management System (RDBMS) that enables you to organize, store, and manage data in structured objects with tables and allows relationships between these tables.
According to the official PostgreSQL download page, PostgreSQL is by default available and supported by all Ubuntu versions. It is one of the reasons why I recommend using this RDBMS over any other open source (RDBMS) options on Ubuntu. Nonetheless, you may have your personal preference/favorite for different reasons.
Let's get to it, shall we?
-
Open your terminal
-
According to the official PostgreSQL download page, you should run the following command:
apt install postgresql
-
Explore more ways to install postgreSQL on Ubuntu for automatic updates throughout the support lifetime of PostgreSQL in the official PostgeSQL download page
- To use PostgreSQL seamlessly, we will need to create a user.
-
Switch to the PostgreSQL Server by running the following command on your terminal:
sudo -i -u postgres
-
To create a user with a password, run the following command (also see option 3 below):
createuser NameOfUserHere -P
-
An alternative (recommended) approach is to use the --interactive flag.
-
Run the following command:
createuser -P --interactive
-
This will prompt you to enter the following:
- Name of the role/user you want to create
- The password and password confirmation for this new user
- Whether to assign a superuser role to this new user (recommended if you haven't created any other user)
-
When you're done creating the new user, type
exit
and pressENTER
to exit the postgres server.
-
After creating a user, you may proceed to create a database.
-
Run the following command to open a postgres shell:
psql
-
Run the following command to create a new database
CREATE DATABASE nameOfYourNewDb;
That's it! You've created your first database which you can use locally to store data.
Check out the official PostgreSQL documentation for more ways that you can use and interact with PostgreSQL.
Check out the following guide to learn how to use your new database in a Python- Django application.