What is PostgreSQL and how do we do a full installation

What is PostgreSQL and how to do a complete installation
What is PostgreSQL and how to do a complete installation

In this article we will look at another Relational Database Management System (RDBMS) the PostgreSQLPostgreSQL is the most advanced open source relational database. As open-source it does not require the purchase of a license and is free.

THE PostgreSQL supports the features ACID like the rest RDBMS with the data being correlated in tables but also NoSQL (non-relational) with the data being in files with a structure like JSON.

In the article we will see step by step how to complete the installation of PostgreSQL on Linux as well as the pgAdmin web tool to manage it.

Installing PostgreSQL

The installation steps in Red Hat Linux It's pretty simple. We'll need to download the repository and run the install.

Also, running initdb will create Database Cluster so that we can manage a collection of databases on a server instance.

sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

sudo yum install -y postgresql11-server

sudo /usr/pgsql-11/bin/postgresql-11-setup initdb

We enable the service so that it starts automatically and start it.

sudo systemctl enable postgresql-11

sudo systemctl start postgresql-11

The basic installation is now complete. To connect to the database for the first time, we will use the postgres user that is created in the database and in the operating system.

sudo -u postgres psql 

With \l we can see the bases that exist at the moment.

What is PostgreSQL and how to do a complete installation

We can easily create a new user with a password and set it superuser so that he has full rights.

create user stratos with password 'kwdikos';

grant all privileges on database postgres to stratos;

alter user stratos with superuser;

With \du We will see the available users and the rights they have.

What is PostgreSQL and how to do a complete installation

Although we created the user, if we try to log in, it won't let us.

What is PostgreSQL and how to do a complete installation

This is because PostgreSQL's default behavior for local connections allows the connection peer which means that for a user to connect, there must also be a user of the operating system connecting to them.

To change this we need to find the pg_hba.confUnfortunately, for security reasons, the only user who has access to find this file in the operating system is the postgres user. With the following command, we find where the file is located so that we can edit it.

sudo -u postgres psql -c 'SHOW hba_file'

sudo vi /var/lib/pgsql/11/data/pg_hba.conf
What is PostgreSQL and how to do a complete installation

Changing it method from peer to md5 now we will be able to connect using a password. Also if on the host change her ip from 127.0.0.1/32 to 0.0.0.0/0 we will be able to connect from another computer wherever it is located.

To do this, however, we will need to look for another file, postgresql.conf.

sudo -u postgres psql -c 'SHOW config_file'


sudo vi  /var/lib/pgsql/11/data/postgresql.conf
What is PostgreSQL and how to do a complete installation

In this file we remove the comment (#) and set listen_addresses = '0.0.0.0' and port = 5432.

A restart is required for these settings to work.

sudo systemctl restart postgresql-11

By doing netstat we should see the ip 0.0.0.0:5432.

netstat -nlp |grep 5432
What is PostgreSQL and how to do a complete installation

We do not forget that for remote access we need to open the door and firewall.

sudo firewall-cmd --zone=public --add-port=5432/tcp --permanent
sudo firewall-cmd --reload

Installing the pgAdmin management tool

After completing the installation in the previous steps, we can install the PostgreSQL administration tool. pgAdmin.

The steps are very simple and we follow them to install the web version that uses apache.

During execution, it will ask us for an email and a password, which will be what we will use to log in to the platform.

sudo rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-1-1.noarch.rpm

sudo yum install pgadmin4-web

sudo /usr/pgadmin4/bin/setup-web.sh

We don't forget to open the firewall on port 80.

sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --reload

Now all we have to do is go to the localhost/pgadmin4 page internally or to the domain we have externally.

What is PostgreSQL and how to do a complete installation

After logging in to add servers, we use the user we created before with a password. For it to work, we must have set pg_hba.conf as a method md5 so we can connect with a code.

What is PostgreSQL and how to do a complete installation

Sources:

Share it.

4 thoughts on “Τι είναι η PostgreSQL και πως κάνουμε μία πλήρης εγκατάσταση”

  1. Good morning, Mr. Stratos, I would like to ask you why in psql when we change SET AUTO COMMIT = “OFF” or 0 do we need to do COMMIT or ROLLBACK even in DDL statements? For example, if I do DROP TABLE IF EXISTS panos RESTRICT (the DEFAULT) or CASCADE and then go to refresh the database for more certainty so that the changes are visible, I will notice that the table will still exist, thus disproving the theory that we only use COMMIT and ROLLBACK in DML statements because when we use DDL statements, COMMIT or ROLLBACK is automatically done because we have in mind from other RDBMS that this is the case, thank you.

      1. Good evening, Mr. Stratos, I would like to ask you something else based on what I had asked you about psql and COMMIT – .etc – (ROLLBACK – SAVEPOINT). As with psql (I imagine) all databases have what we call {the problem (for RDBMS only)} -> LOCK, BLOCK, DEADLOCK? Thank you.

        1. Regarding transactions, for acid reasons, their cycle applies to all rdbms for security reasons. However, you can use isolation level elsewhere but you will have dirty reads. Regarding Vacuum, it updates the statistics and frees the reserved space, it does nothing else.

Leave a reply