What is postgresql? Is a database engine. What is a database? A way to store data in an indexed order. I will tell you before I begin here. I am not a DBA. (database administrator) this is not really my forte. I know enough SQL to be dangerous. I'm going to keep this pretty basic. Once you get familiar with databases and SQL (structured query language) you will want to do more advanced stuff like relational tables, foreign keys, left joins and other things. This tutorial will not cover those things. This is basic installation, basic use, and introduction to SQL language.
SQL databses aren't really a direct Linux skill per se. But many people who run Linux computer run SQL databases. There are
other types of databases called no-SQL as well. That's another subject.
Why postgresql? Mostly because it's what I am most familiar with with, I have ran OracleDB, MariaDB, and MySQL in the past. Oracle bought out MySQL several years back. It was branched into MariaDB. There's nothing wrong with those databases, if you like them, by all means use them. Postgresql is of an enterprise level database, it has more features and scales well.
So how do we install it? For my distro it looks like this.
Fedora/Redhat/Rocy
Debain/Ubuntu/Mint
Arch/Endeavor
By deffault my distro wants to install postgresql16, but 17 is in the repos, so I am using that. The version doesn't matter that much for this tutorial. The package called postgresql17 isn't really the database, it's the client you use to connect to the database. postgresql17-server is the actual server. The other two packages are optional, but they come in handy for many situations. Most SQL databases can use something called a "stored procedure". Often these are written in python or perl or tcl, and a couple of other languages. I tend to mostly use python these days, but feel free to install any add-ons you like.
Usually when you install postgresql, it will create a user called "postgres" on your system. You will need to know the password for this user. Make sure you write it down or remember it somehow. You can always change it again if you forget, but... (more about that later). Usually the first thing I do after installing postgresql is...
If you get ahead of me here and try to start postgresql with something like...
It probably won't start, you'll likely get some errors. There are a few things we need to do first. Depending on your distro, postgresql won't let you do some of these things as root. You'll have to become the postgres user. Befire we do that, lets fix some permissions.
Once you are the postgres user, run this command. Usually postgsql puts it's data in /var/lib/pgsql by default. You can chnage this, but
for now, we'll just go with the default location.
You will some stuff scroll by on the screen. At the end of this output it will tell you how to start postgresql. I recommend that you don't
do it that way. Instead, I prefer the systemd method.
You can exit being the postgres user for a bit here now. As root we can enable and start postgresql.
Now you shouldn't get any errors, it should start normally at this point. Congratulations your database engine is up and running.
Next... we will login and create a database.
... to be continued.
SQL databses aren't really a direct Linux skill per se. But many people who run Linux computer run SQL databases. There are
other types of databases called no-SQL as well. That's another subject.
Why postgresql? Mostly because it's what I am most familiar with with, I have ran OracleDB, MariaDB, and MySQL in the past. Oracle bought out MySQL several years back. It was branched into MariaDB. There's nothing wrong with those databases, if you like them, by all means use them. Postgresql is of an enterprise level database, it has more features and scales well.
So how do we install it? For my distro it looks like this.
Fedora/Redhat/Rocy
Code:
dnf install -y postgresql17 postgresql17-contrib postgresql17-plpython3 postgresql17-server
Bash:
sudo apt update
sudo apt install -y postgresql-17 postgresql-contrib postgresql-plpython3 postgresql-server
Code:
sudo pacman -Syu
sudo pacman -S postgresql postgresql-contrib postgresql-plpython postgresql-server
By deffault my distro wants to install postgresql16, but 17 is in the repos, so I am using that. The version doesn't matter that much for this tutorial. The package called postgresql17 isn't really the database, it's the client you use to connect to the database. postgresql17-server is the actual server. The other two packages are optional, but they come in handy for many situations. Most SQL databases can use something called a "stored procedure". Often these are written in python or perl or tcl, and a couple of other languages. I tend to mostly use python these days, but feel free to install any add-ons you like.
Usually when you install postgresql, it will create a user called "postgres" on your system. You will need to know the password for this user. Make sure you write it down or remember it somehow. You can always change it again if you forget, but... (more about that later). Usually the first thing I do after installing postgresql is...
Bash:
sudo passwd postgres
If you get ahead of me here and try to start postgresql with something like...
Bash:
systemctl start postgresql
It probably won't start, you'll likely get some errors. There are a few things we need to do first. Depending on your distro, postgresql won't let you do some of these things as root. You'll have to become the postgres user. Befire we do that, lets fix some permissions.
Bash:
chown -R postgres /var/lib/pgsql
Bash:
sudo su - postgres
Once you are the postgres user, run this command. Usually postgsql puts it's data in /var/lib/pgsql by default. You can chnage this, but
for now, we'll just go with the default location.
Bash:
initdb -D /var/lib/pgsql/data
You will some stuff scroll by on the screen. At the end of this output it will tell you how to start postgresql. I recommend that you don't
do it that way. Instead, I prefer the systemd method.
You can exit being the postgres user for a bit here now. As root we can enable and start postgresql.
Bash:
systemctl enable postgresql
systemctl start postgresql
Now you shouldn't get any errors, it should start normally at this point. Congratulations your database engine is up and running.
Next... we will login and create a database.
... to be continued.
Last edited: