custom background image

So installieren Sie eine PostgreSQL-Datenbank auf einem CentOS-7-Server


PostgreSQL-Datenbank auf CentOS 7 installieren

Das Ziel

PostgreSQL ist eine der bekanntesten Datenbanken der Welt. Ihre große Beliebtheit geht in erster Linie auf die Benutzerfreundlichkeit und den Open-Source-Ansatz zurück. Mehr Informationen zu den Möglichkeiten mit PostgreSQL finden Sie in der offiziellen Dokumentation.

In diesem Tutorial erfahren Sie, wie Sie eine PostgreSQL-Datenbank auf der Linux-Distribution CentOS 7 installieren.

Voraussetzungen

Dieses Tutorial setzt eine OVHcloud Public Cloud Compute Instanz, einen VPS oder einen Bare-Metal-Server mit CentOS 7 voraus, sowie Grundkenntnisse im Umgang mit der Kommandozeile. In diesem Tutorial verwenden wir eine Public Cloud Compute Instanz. Sie brauchen Hilfe bei der Einrichtung einer Public Cloud Instanz mit CentOS 7? Dann lesen Sie folgende Anleitung: Erste Public Cloud Instanz erstellen und auf dieser einloggen.

Anleitung

In diesem Tutorial werden Sie eine PostgreSQL-Datenbank installieren und anschließend eine Datenbank erstellen.

 

PostgreSQL-Datenbank installieren

Zum Zeitpunkt der Verfassung dieses Tutorials war PostgreSQL 14.x die aktuelle Version. Die verfügbaren Versionen finden Sie im Download-Bereich der offiziellen Dokumentation.

Stellen Sie zunächst sicher, dass Ihr System auf dem neuesten Stand ist:

sudo yum update

Installieren Sie das RPM-Repository:

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

Output:

$ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Modules complémentaires chargés : fastestmirror
pgdg-redhat-repo-latest.noarch.rpm                       | 8.1 kB     00:00
Examen de /var/tmp/yum-root-4xEZ6S/pgdg-redhat-repo-latest.noarch.rpm : pgdg-redhat-repo-42.0-24.noarch
Sélection de /var/tmp/yum-root-4xEZ6S/pgdg-redhat-repo-latest.noarch.rpm pour installation
Résolution des dépendances
--> Lancement de la transaction de test
---> Le paquet pgdg-redhat-repo.noarch 0:42.0-24 sera installé
--> Résolution des dépendances terminée

Dépendances résolues

================================================================================
 Package            Architecture
                             Version    Dépôt                             Taille
================================================================================
Installation :
 pgdg-redhat-repo   noarch   42.0-24    /pgdg-redhat-repo-latest.noarch    11 k

Résumé de la transaction
================================================================================
Installation   1 Paquet

Taille totale  : 11 k
Taille d'installation : 11 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installation : pgdg-redhat-repo-42.0-24.noarch                            1/1
  Vérification : pgdg-redhat-repo-42.0-24.noarch                            1/1

Installé :
  pgdg-redhat-repo.noarch 0:42.0-24

Terminé !

Installieren Sie nun PostgreSQL:

sudo yum install -y postgresql14-server

$ sudo yum install -y postgresql14-server
Modules complémentaires chargés : fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.proxad.net
 * extras: centos.mirrors.proxad.net
 * updates: centos.mirrors.proxad.net
pgdg-common/7/x86_64/signature                           |  198 B     00:00
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg-common/7/x86_64/signature                           | 2.9 kB     00:00 !!!
pgdg10/7/x86_64/signature                                |  198 B     00:00
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg10/7/x86_64/signature                                | 3.6 kB     00:00 !!!
pgdg11/7/x86_64/signature                                |  198 B     00:00
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg11/7/x86_64/signature                                | 3.6 kB     00:00 !!!
pgdg12/7/x86_64/signature                                |  198 B     00:00
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg12/7/x86_64/signature                                | 3.6 kB     00:00 !!!
pgdg13/7/x86_64/signature                                |  198 B     00:00
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg13/7/x86_64/signature                                | 3.6 kB     00:00 !!!
pgdg14/7/x86_64/signature                                |  198 B     00:00
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
pgdg14/7/x86_64/signature                                | 3.6 kB     00:00 !!!
(1/11): pgdg11/7/x86_64/group_gz                           |  245 B   00:00
(2/11): pgdg10/7/x86_64/group_gz                           |  245 B   00:00
(3/11): pgdg12/7/x86_64/group_gz                           |  245 B   00:00
(4/11): pgdg13/7/x86_64/group_gz                           |  246 B   00:00
(5/11): pgdg-common/7/x86_64/primary_db                    | 162 kB   00:00
(6/11): pgdg10/7/x86_64/primary_db                         | 371 kB   00:01
(7/11): pgdg14/7/x86_64/group_gz                           |  244 B   00:00
(8/11): pgdg11/7/x86_64/primary_db                         | 400 kB   00:01
(9/11): pgdg14/7/x86_64/primary_db                         |  98 kB   00:00
(10/11): pgdg12/7/x86_64/primary_db                        | 277 kB   00:00
(11/11): pgdg13/7/x86_64/primary_db                        | 181 kB   00:00
Résolution des dépendances
--> Lancement de la transaction de test
---> Le paquet postgresql14-server.x86_64 0:14.2-1PGDG.rhel7 sera installé
--> Traitement de la dépendance : postgresql14-libs(x86-64) = 14.2-1PGDG.rhel7 pour le paquet : postgresql14-server-14.2-1PGDG.rhel7.x86_64
--> Traitement de la dépendance : postgresql14(x86-64) = 14.2-1PGDG.rhel7 pour le paquet : postgresql14-server-14.2-1PGDG.rhel7.x86_64
--> Traitement de la dépendance : libpq.so.5()(64bit) pour le paquet : postgresql14-server-14.2-1PGDG.rhel7.x86_64
--> Traitement de la dépendance : libicuuc.so.50()(64bit) pour le paquet : postgresql14-server-14.2-1PGDG.rhel7.x86_64
--> Traitement de la dépendance : libicui18n.so.50()(64bit) pour le paquet : postgresql14-server-14.2-1PGDG.rhel7.x86_64
--> Lancement de la transaction de test
---> Le paquet libicu.x86_64 0:50.2-4.el7_7 sera installé
---> Le paquet postgresql14.x86_64 0:14.2-1PGDG.rhel7 sera installé
---> Le paquet postgresql14-libs.x86_64 0:14.2-1PGDG.rhel7 sera installé
--> Résolution des dépendances terminée

Dépendances résolues

================================================================================
 Package                  Architecture
                                      Version                 Dépôt       Taille
================================================================================
Installation :
 postgresql14-server      x86_64      14.2-1PGDG.rhel7        pgdg14      5.5 M
Installation pour dépendances :
 libicu                   x86_64      50.2-4.el7_7            base        6.9 M
 postgresql14             x86_64      14.2-1PGDG.rhel7        pgdg14      1.5 M
 postgresql14-libs        x86_64      14.2-1PGDG.rhel7        pgdg14      267 k

Résumé de la transaction
================================================================================
Installation   1 Paquet (+3 Paquets en dépendance)

Taille totale des téléchargements : 14 M
Taille d'installation : 55 M
Downloading packages:
(1/4): libicu-50.2-4.el7_7.x86_64.rpm                      | 6.9 MB   00:00
warning: /var/cache/yum/x86_64/7/pgdg14/packages/postgresql14-libs-14.2-1PGDG.rhel7.x86_64.rpm: Header V4 DSA/SHA1 Signature, key ID 442df0f8: NOKEY
La clé publique pour postgresql14-libs-14.2-1PGDG.rhel7.x86_64.rpm n'est pas installée
(2/4): postgresql14-libs-14.2-1PGDG.rhel7.x86_64.rpm       | 267 kB   00:01
(3/4): postgresql14-14.2-1PGDG.rhel7.x86_64.rpm            | 1.5 MB   00:01
(4/4): postgresql14-server-14.2-1PGDG.rhel7.x86_64.rpm     | 5.5 MB   00:00
--------------------------------------------------------------------------------
Total                                              7.5 MB/s |  14 MB  00:01
Récupération de la clé à partir de file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Importation de la clef GPG 0x442DF0F8 :
ID utilisateur : "PostgreSQL RPM-Bauprojekt "
Empreinte : 68c9 e2b9 1a37 d136 fe74 d176 1f16 d2e1 442d f0f8
Paquet         : pgdg-redhat-repo-42.0-24.noarch (@/pgdg-redhat-repo-latest.noarch)
Provient de    : /etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installation: postgresql14-libs-14.2-1PGDG.rhel7.x86_64                  1/4
  Installation: libicu-50.2-4.el7_7.x86_64                                 2/4
  Installation: postgresql14-14.2-1PGDG.rhel7.x86_64                       3/4
  Installation: postgresql14-server-14.2-1PGDG.rhel7.x86_64                4/4
  Vérification : libicu-50.2-4.el7_7.x86_64                                 1/4
  Vérification : postgresql14-libs-14.2-1PGDG.rhel7.x86_64                  2/4
  Vérification : postgresql14-server-14.2-1PGDG.rhel7.x86_64                3/4
  Vérification : postgresql14-14.2-1PGDG.rhel7.x86_64                       4/4

Installé :
  postgresql14-server.x86_64 0:14.2-1PGDG.rhel7

Dépendances installées :
  libicu.x86_64 0:50.2-4.el7_7
  postgresql14.x86_64 0:14.2-1PGDG.rhel7
  postgresql14-libs.x86_64 0:14.2-1PGDG.rhel7

Terminé !

Initialisieren Sie die Datenbank und aktivieren Sie den automatischen Start:

sudo /usr/pgsql-14/bin/postgresql-14-setup initdb sudo systemctl enable postgresql-14 sudo systemctl start postgresql-14

Testen Sie anschließend Ihre Installation:

sudo su - postgres psql

Output:

$ sudo su - postgres
-bash-4.2$ psql
psql (14.2)
Type "help" for help.

postgres=# exit
-bash-4.2$ exit
$

Beachten Sie, dass bei der Installation von PostgreSQL standardmäßig ein postgres-Account erstellt wird. Dieser Account ist ein Superuser-Account für die Datenbank. Verwenden Sie diesen mit Bedacht.

 

Benutzer zu PostgreSQL hinzufügen

Es gibt zwei Möglichkeiten, einen Benutzer in PostgreSQL hinzuzufügen: per SQL-Abfrage oder per Befehl in der Bash. In diesem Tutorial werden wir nur die Vorgehensweise per SQL verwenden, weil sie umfassender ist. Informationen zur Verwendung des bash-Befehls finden Sie in der offiziellen Dokumentation. Um Ihren neuen Account zu erstellen, müssen Sie den postgres-Standard-Account und die psql-CLI verwenden.

Das Ziel ist, eine Rolle zu erstellen (eine Art Kombination von Benutzern und Rechten in der PostgreSQL-Welt):

postgres=# CREATE USER foo WITH PASSWORD 'bar';

Output:

postgres=# CREATE USER foo WITH PASSWORD 'bar';
CREATE ROLE
postgres=#

 

Datenbank erstellen

Erstellen Sie eine Datenbank mit dem Namen „example“ und dem Benutzer „foo“ als Besitzer:

postgres=# CREATE DATABASE example OWNER foo;
CREATE DATABASE
postgres=#

Um Ihren neuen Benutzeraccount zu verwenden, muss zuerst ein entsprechender Linux-Benutzer erstellt werden (für diese Art von Befehl sind Root-Rechte erforderlich):

$ sudo adduser foo

Output:

$ sudo su -
Dernière connexion : jeudi 12 mai 2022 à 09:42:40 UTC sur pts/0
# adduser foo
# exit
$

Testen Sie Ihren neuen Account:

sudo su - foo psql -d example

Output:

$ sudo su - foo
Dernière connexion : jeudi 12 mai 2022 à 10:09:42 UTC sur pts/0
$ psql -d example
psql (14.2)
Type "help" for help.

example=>

 Listen Sie die Datenbanken auf:

postgres=# \l

Output:

postgres=# \l
example=> \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
 example   | foo      | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

 

Tabelle erstellen und verwenden

Erstellen Sie Tabellen und verwenden Sie diese.

Verwenden Sie für die folgenden Befehle den zuvor erstellten foo-Benutzer.

example=> CREATE TABLE first_table (id INT PRIMARY KEY NOT NULL, column1 CHAR(50));

Output:

example=> CREATE TABLE first_table (id INT PRIMARY KEY NOT NULL, column1 CHAR(50));
CREATE TABLE
example=> 

Sie können jetzt die erste Tabelle first_table in SQL-Abfragen verwenden:

example=> INSERT INTO first_table (id, column1) VALUES (1, 'example'); example=> SELECT * FROM first_table;

Output:

example=> INSERT INTO first_table (id, column1) VALUES (1, 'example');
INSERT 0 1

example=> SELECT * FROM first_table;
 id |                      column1                       
----+----------------------------------------------------
  1 | example                                           
(1 row)

Geschafft! Sie haben erfolgreich eine PostgreSQL-Datenbank auf CentOS 7 installiert und konfiguriert.