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
-
But you may run into a Permission denied error:
-
In that case, run the command as a superuser with
sudo
:sudo 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
-
Replace NameOfUserHere with your preferred name for the new user.
-
You will be prompted to enter a password for the new user and confirm it.
-
When you're done creating the new user, type
exit
and pressENTER
to exit the postgres server.
- 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, you will need to create an initial database that matches the name of your new user. Otherwise, you will not be able to access the postgreSQL
shell.
For instance, if above you created a user called benie
, your initial database should be called benie
.
Follow the steps below to create your initial database:
- Run the
PostgreSQL
Server
-
In case you had terminated your server session, run the following command to restart it:
sudo -i -u postgres
- Create an Initial Database
-
Create your initial database using the same name you used for your new user:
createdb NameOfYourNewUserHere;
-
Replace NameOfYourNewUserHere with the name you used to create your new user in (3) above, e.g.
benie
. -
Once you've created your initial database, type
exit
and pressENTER
to exit the postgres server.
After creating a user with an initial database, you will have access to the postgreSQL shell and may proceed to create more databases.
-
Run the following command to open a postgreSQL shell:
psql
-
Run the following command to create a new database
CREATE DATABASE nameOfYourNewDb;
-
Replace nameOfYourDb with your preferred name for your database.
-
Please remember to add a semicolon
;
after each line. -
When you've created your new database, type
\q
and pressENTER
to exit the postgres shell.
That's it! You've successfully:
- Created a postgreSQL user
- Your initial database
- Your first subsequent database (after the initial)
You can use always use your subsequent databases 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.