GNU/Linux >> Linux Esercitazione >  >> Linux

Come installare e proteggere PostgreSQL Server su RockyLinux 8

PostgreSQL è un popolare sistema di gestione di database relazionali open source. Noto per la sua affidabilità, sta guadagnando popolarità grazie alla sua robustezza, flessibilità e prestazioni. PostgreSQL è utilizzato per la gestione di database e numerose applicazioni web e analitiche. Al momento della stesura di questo articolo, PostgreSQL 13 è l'ultima versione. Questa versione include miglioramenti significativi al sistema di indicizzazione e ricerca a vantaggio dei database di grandi dimensioni.

In questo post, ti mostreremo come installare e proteggere PostgreSQL su Rocky Linux 8.

Prerequisiti

  • Un server che esegue Rocky Linux 8 sulla piattaforma Atlantic.Net Cloud
  • Una password di root configurata sul tuo server

Fase 1:crea un server cloud Atlantic.Net

Per prima cosa, accedi al tuo server Atlantic.Net Cloud. Crea un nuovo server, scegliendo Rocky Linux 8 come sistema operativo con almeno 2 GB di RAM. Collegati al tuo Cloud Server tramite SSH e accedi utilizzando le credenziali evidenziate in alto nella pagina.

Una volta effettuato l'accesso al server, eseguire il comando seguente per aggiornare il sistema di base con gli ultimi pacchetti disponibili.

dnf update -y

Passaggio 2:aggiungi il repository PostgreSQL 13

dnf module list postgresql

Dovresti vedere che è disponibile solo PostgreSQL versione 10:

Rocky Linux 8 - AppStream
Name                         Stream                   Profiles                             Summary                                             
postgresql                   9.6                      client, server [d]                   PostgreSQL server and client module                 
postgresql                   10 [d]                   client, server [d]                   PostgreSQL server and client module                 
postgresql                   12                       client, server [d]                   PostgreSQL server and client module                 
postgresql                   13                       client, server [d]                   PostgreSQL server and client module                 

Per installare l'ultima versione di PostgreSQL, dovrai installare il repository PostgreSQL sul tuo sistema.

Puoi installarlo usando il seguente comando:

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

Una volta creato il repository, puoi procedere al passaggio successivo.

Fase 3:installa PostgreSQL 13 su Rocky Linux 8

Ora aggiorna il tuo repository usando il seguente comando:

dnf update -y

Quindi, disabilita il repository PostgreSQL predefinito usando il comando seguente:

dnf -qy module disable postgresql

Quindi, installa l'ultima versione di PostgreSQL eseguendo il comando seguente:

dnf install postgresql13 postgresql13-server

Una volta installato PostgreSQL 13, otterrai il seguente output:

Last metadata expiration check: 0:00:08 ago on Fri 22 Oct 2021 08:38:58 AM UTC.
Dependencies resolved.
===============================================================================================================================================
 Package                                  Architecture                Version                                Repository                   Size
===============================================================================================================================================
Installing:
 postgresql13                             x86_64                      13.4-1PGDG.rhel8                       pgdg13                      1.5 M
 postgresql13-server                      x86_64                      13.4-1PGDG.rhel8                       pgdg13                      5.5 M
Installing dependencies:
 postgresql13-libs                        x86_64                      13.4-1PGDG.rhel8                       pgdg13                      414 k

Transaction Summary
===============================================================================================================================================
Install  3 Packages

Total download size: 7.4 M
Installed size: 31 M
Is this ok [y/N]: y

Quindi, inizializza il database PostgreSQL con il seguente comando:

/usr/pgsql-13/bin/postgresql-13-setup initdb

Esempio di output:

Initializing database ... OK

Quindi, avvia il servizio PostgreSQL e abilitalo all'avvio al riavvio del sistema con il seguente comando:

systemctl start postgresql-13
systemctl enable postgresql-13

Puoi controllare lo stato di PostgreSQL con il seguente comando:

systemctl status postgresql-13

Dovresti ottenere il seguente output:

● postgresql-13.service - PostgreSQL 13 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-13.service; disabled; vendor preset: disabled)
   Active: active (running) since Fri 2021-10-22 08:39:47 UTC; 7s ago
     Docs: https://www.postgresql.org/docs/13/static/
  Process: 36412 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 36417 (postmaster)
    Tasks: 8 (limit: 11411)
   Memory: 16.8M
   CGroup: /system.slice/postgresql-13.service
           ├─36417 /usr/pgsql-13/bin/postmaster -D /var/lib/pgsql/13/data/
           ├─36419 postgres: logger 
           ├─36421 postgres: checkpointer 
           ├─36422 postgres: background writer 
           ├─36423 postgres: walwriter 
           ├─36424 postgres: autovacuum launcher 
           ├─36425 postgres: stats collector 
           └─36426 postgres: logical replication launcher 
ss -antpl | grep 5432

Otterrai il seguente output:

LISTEN 0      128        127.0.0.1:5432       0.0.0.0:*    users:(("postmaster",pid=36417,fd=7))
LISTEN 0      128            [::1]:5432          [::]:*    users:(("postmaster",pid=36417,fd=6))

Fase 4:imposta una password per l'utente Postgres

Per impostare una password, accedi a PostgreSQL con il seguente comando:

su - postgres

Quindi, imposta una password sicura con il seguente comando:

psql -c "alter user postgres with password 'securepassword'"

Quindi, esci dalla shell di PostgreSQL usando il seguente comando:

exit

Passaggio 5:modifica del metodo di autenticazione PostgreSQL

Puoi cambiarlo modificando il file di configurazione principale di PostgreSQL:

nano /var/lib/pgsql/13/data/pg_hba.conf

Trova la seguente riga:

local   all             all                                     peer

E sostituiscilo con la seguente riga:

local   all             all                                     scram-sha-256

Salva e chiudi il file, quindi riavvia il servizio PostgreSQL per applicare le modifiche.

systemctl restart postgresql-13

Fase 6:crea un database e un utente in PostgreSQL

Innanzitutto, accedi alla shell di PostgreSQL con il seguente comando:

sudo -u postgres psql

Otterrai il seguente output:

could not change directory to "/root": Permission denied
psql (13.4)
Type "help" for help.

postgres=# 

Quindi, crea un nuovo utente PostgreSQL chiamato user1 usando il comando seguente:

CREATE USER user1 WITH CREATEDB CREATEROLE PASSWORD 'passoword';

Per verificare gli utenti PostgreSQL, esegui:

\du

Otterrai il seguente output:

                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 user1     | Create role, Create DB                                     | {}

Per creare un nuovo database PostgreSQL chiamato user1db, esegui:

CREATE DATABASE user1db OWNER user1;

Per verificare i database PostgreSQL, esegui:

\l

Otterrai il seguente output:

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 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
 user1db   | user1    | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 

Conclusione

Congratulazioni! Hai installato e protetto correttamente PostgreSQL su Rocky Linux 8. Per motivi di sicurezza, si consiglia sempre di installare l'ultima versione di PostgreSQL nell'ambiente di produzione. Provalo con l'hosting VPS di Atlantic.Net!


Linux
  1. Come installare e configurare Postgres 13 su Centos 8

  2. Come installare e configurare Postgres 14 su Fedora 34

  3. Come installare e proteggere Redis su Ubuntu 18.04

  4. Come proteggere PostgreSQL Server

  5. Come installare e proteggere MongoDB 4 in CentOS 8

Come installare il database PostgreSQL e pgAdmin su Linux

Come installare e proteggere phpMyAdmin in Ubuntu 14.04

Come installare Tomcat e Java su CentOS 8

Come installare e proteggere Memcached su Ubuntu 18.04

Come installare e proteggere phpMyAdmin su CentOS 8

Come installare e configurare Parse Server su Ubuntu 20.04