6.5.4 PostgreSQL: introduction and installation

Postgre Banner EN

PostgreSQL is a powerful, open-source, object-relational database management system (RDBMS) designed for storing, processing, and managing large amounts of data. Designed with extensibility and SQL standard compatibility in mind, PostgreSQL supports both relational and some NoSQL features, such as JSON and key-value storage. Its primary purpose is to provide a secure and reliable data storage solution that allows users to efficiently manage complex transactions and perform real-time data analytics.

Major Pros and Cons:
  • High SQL compatibility. PostgreSQL supports virtually all features of standard SQL, making it convenient for developing complex queries and transactions.
  • Reliability and ACID compliance. ACID (Atomicity, Consistency, Isolation, Durability) support makes PostgreSQL robust and predictable when processing mission-critical data.
  • Extensibility. PostgreSQL allows users to add their own features, which is useful for specific tasks and customization to unique requirements.
  • JSON and NoSQL support. In addition to relational data, PostgreSQL also supports JSON data, making it suitable for hybrid applications that require both relational and document-oriented data.
  • Configuration complexity. PostgreSQL sometimes requires more complex configuration and management compared to other DBMSs, which may require administrative experience.
  • Resource intensity. Due to its wide range of features and compatibility, PostgreSQL can consume more resources than other DBMSs, especially when processing large volumes of data.

Using PostgreSQL is justified when a project needs a stable, scalable database with advanced SQL capabilities and flexibility for a variety of data.

Installation on Linux

In this example, we will install PostgreSQL on Ubuntu 22.04. This process consists of standard steps: updating the system, adding the official PostgreSQL repository, installing and configuring the database. We will also consider possible nuances that you may encounter.

1. Update the system. Before installing PostgreSQL, make sure that the packages are updated to the current version. This helps to avoid compatibility errors:

sudo apt update && sudo apt upgrade -y

2. Add the official PostgreSQL repository. Ubuntu 22.04 may not include the latest version of PostgreSQL in its default repositories, so it makes sense to add the official PostgreSQL repository:

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

3. Add the repository key. Without adding the security key, installing packages may fail:

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

4. Now update your package list to include the PostgreSQL repository:

sudo apt update

5. Install PostgreSQL, specifying the version you want (replace 14 with another version if you want to install it):

sudo apt install postgresql-14 postgresql-contrib

Note: The postgresql-contrib package installs additional extensions for PostgreSQL, such as pgcrypto for encryption and tablefunc for advanced table handling.

6. Check the service status:

After installation, the PostgreSQL service should start automatically. Check its status:

sudo systemctl status postgresql

If the service does not start, try starting it with the command:

sudo systemctl start postgresql

7. PostgreSQL runs under the postgres user by default. To switch to this user and run the psql client, use:

sudo -i -u postgres
psql

8. Inside the psql client interface, you can create new databases and users for easy management. An example of creating a user and database:

CREATE USER exampleuser WITH PASSWORD 'examplepassword';
CREATE DATABASE exampledb OWNER exampleuser;

9. By default, PostgreSQL only accepts local connections. Edit the postgresql.conf configuration file to allow external connections:

sudo nano /etc/postgresql/14/main/postgresql.conf

In the listen_addresses line, replace localhost with * to make PostgreSQL listen to requests from any IP:

listen_addresses = '*'

Additional: * will allow all available IP interfaces (IPv4 and IPv6) to listen to requests. To listen only on IPv4, set 0.0.0.0, while :: allows listening on all IPv6 addresses.

Then open the pg_hba.conf file to specify the allowed IP addresses for access:

sudo nano /etc/postgresql/14/main/pg_hba.conf

Add the following line to allow external connections with a password:

host all all 0.0.0.0/0 md5

10. To apply the changes, restart PostgreSQL:

sudo systemctl restart postgresql

This completes the installation and basic configuration of PostgreSQL on Ubuntu 22.04. You have a working DBMS with a created database and user and allowed external connections (with a password).

Advice: You can read more about remote work with the database in this article.

Installing on Windows

1. Download the installer – go to the official site PostgreSQL and select the latest installer for Windows. This is usually an .exe file.

2. Double-click the downloaded file to start the installation process. The installer will open the installation wizard.

3. By default, PostgreSQL will be installed in C:\Program Files\PostgreSQL\ with a folder for the version. You can change the path if necessary, but we recommend sticking to the default path unless absolutely necessary.

4. The installer will prompt you to select the components to install:

Postgre Installer Components

  • PostgreSQL Server – required for operation.
  • pgAdmin – graphical interface for managing and working with the database.
  • Command Line Tools – useful for working in the command line.
  • Stack Builder – a tool for installing additional plugins and extensions.

Usually, you can select all components without thinking.

5. Set the superuser password postgres The installer will ask for a password for the postgres account. Remember it – this is the main account with access to all database features.

6. By default, PostgreSQL uses port 5432. If this port is already in use, select another free port.

7. The installer will prompt you to select locale and region settings that affect how the database is sorted and compared. Usually, you can leave the default values.

8. After checking the settings, click Next to start the installation. Wait for the installation to complete and click Finish.

Starting and connecting via pgAdmin

After installation, PostgreSQL will automatically start as a Windows service, and you can manage it via pgAdmin, which will also be installed on your computer.

Run pgAdmin, add the PostgreSQL server using localhost and the port you specified earlier (5432 by default), and enter the postgres superuser password.

PGAdmin, appearance
Appearance of the pgAdmin

If everything went well, from this point PostgreSQL is installed and ready to work.