PostgreSQL
 sql >> Database >  >> RDS >> PostgreSQL

Come installare PostgreSQL 12 su Ubuntu 20.04/18.04/16.04

In questo articolo, ti mostreremo come installare PostgreSQL 12 su Ubuntu 20.04/18.04/16.04. PostgreSQL è un sistema di database open source ed è potente come altri sistemi di database relazionali.

Puoi visualizzare la pagina di rilascio di postgreSQL 12. 

Passaggi per installare PostgreSQL 12 su Ubuntu 

Passaggio 1:aggiorna il sistema Ubuntu

Si consiglia sempre di aggiornare il sistema prima di installare PostgreSQL.

sudo apt update

Passaggio 2:installa il pacchetto richiesto 

sudo apt -y install vim bash-completion wget

Se il pacchetto è già installato, vedrai l'output sotto, puoi ignorarlo.

Esempio di output:

root@PostgreSQL:~# sudo apt -y install vim bash-completion wget
Reading package lists... Done
Building dependency tree 
Reading state information... Done
bash-completion is already the newest version (1:2.11-2ubuntu1).
vim is already the newest version (2:8.2.0716-3ubuntu2).
wget is already the newest version (1.20.3-1ubuntu1).
wget set to manually installed.
The following package was automatically installed and is no longer required:
virtualbox-guest-utils
Use 'sudo apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 212 not upgraded.
root@PostgreSQL:~# 

Fase 3:aggiungi il repository PostgreSQL 12

wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" |sudo tee  /etc/apt/sources.list.d/pgdg.list

Passaggio 4:installa PostgreSQL 12 su Ubuntu

sudo apt update

sudo apt -y install postgresql-12 postgresql-client-12

Esempio di output:

root@PostgreSQL:~# sudo apt -y install postgresql-12 postgresql-client-12
Reading package lists... Done
Building dependency tree 
Reading state information... Done
The following package was automatically installed and is no longer required:
virtualbox-guest-utils
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
libcommon-sense-perl libjson-perl libjson-xs-perl libpq5
libtypes-serialiser-perl pgdg-keyring postgresql-client-common
postgresql-common sysstat
Suggested packages:
postgresql-doc-12 isag
The following NEW packages will be installed:
libcommon-sense-perl libjson-perl libjson-xs-perl libpq5
libtypes-serialiser-perl pgdg-keyring postgresql-12 postgresql-client-12
postgresql-client-common postgresql-common sysstat
0 upgraded, 11 newly installed, 0 to remove and 212 not upgraded.
Need to get 17.5 MB of archives.
After this operation, 59.4 MB of additional disk space will be used.
Get:1 http://apt.postgresql.org/pub/repos/apt groovy-pgdg/main amd64 libpq5 amd64 13.3-1.pgdg20.10+1 [177 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu groovy/main amd64 libcommon-sense-perl amd64 3.75-1build2 [20.5 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu groovy/main amd64 libjson-perl all 4.02000-2 [80.9 kB]
Get:4 http://apt.postgresql.org/pub/repos/apt groovy-pgdg/main amd64 pgdg-keyring all 2018.2 [10.7 kB]
Get:5 http://apt.postgresql.org/pub/repos/apt groovy-pgdg/main amd64 postgresql-client-common all 226.pgdg20.10+1 [90.6 kB]
Get:6 http://us.archive.ubuntu.com/ubuntu groovy/main amd64 libtypes-serialiser-perl all 1.0-1 [12.1 kB]
Get:7 http://us.archive.ubuntu.com/ubuntu groovy/main amd64 libjson-xs-perl amd64 4.020-1build1 [83.7 kB]
Get:8 http://apt.postgresql.org/pub/repos/apt groovy-pgdg/main amd64 postgresql-client-12 amd64 12.7-1.pgdg20.10+1 [1,429 kB]
Get:9 http://us.archive.ubuntu.com/ubuntu groovy/main amd64 sysstat amd64 12.4.0-1 [471 kB]
Get:10 http://apt.postgresql.org/pub/repos/apt groovy-pgdg/main amd64 postgresql-common all 226.pgdg20.10+1 [246 kB]
Get:11 http://apt.postgresql.org/pub/repos/apt groovy-pgdg/main amd64 postgresql-12 amd64 12.7-1.pgdg20.10+1 [14.9 MB]
Fetched 17.5 MB in 15s (1,141 kB/s) 
Preconfiguring packages ...
Selecting previously unselected package libcommon-sense-perl.
(Reading database ... 192977 files and directories currently installed.)
Preparing to unpack .../00-libcommon-sense-perl_3.75-1build2_amd64.deb ...
Unpacking libcommon-sense-perl (3.75-1build2) ...
Selecting previously unselected package libjson-perl.
Preparing to unpack .../01-libjson-perl_4.02000-2_all.deb ...
Unpacking libjson-perl (4.02000-2) ...
Selecting previously unselected package libtypes-serialiser-perl.
Preparing to unpack .../02-libtypes-serialiser-perl_1.0-1_all.deb ...
Unpacking libtypes-serialiser-perl (1.0-1) ...
Selecting previously unselected package libjson-xs-perl.
Preparing to unpack .../03-libjson-xs-perl_4.020-1build1_amd64.deb ...
Unpacking libjson-xs-perl (4.020-1build1) ...
Selecting previously unselected package libpq5:amd64.
Preparing to unpack .../04-libpq5_13.3-1.pgdg20.10+1_amd64.deb ...
Unpacking libpq5:amd64 (13.3-1.pgdg20.10+1) ...
Selecting previously unselected package pgdg-keyring.
Preparing to unpack .../05-pgdg-keyring_2018.2_all.deb ...
Unpacking pgdg-keyring (2018.2) ...
Selecting previously unselected package postgresql-client-common.
Preparing to unpack .../06-postgresql-client-common_226.pgdg20.10+1_all.deb ...
Unpacking postgresql-client-common (226.pgdg20.10+1) ...
Selecting previously unselected package postgresql-client-12.
Preparing to unpack .../07-postgresql-client-12_12.7-1.pgdg20.10+1_amd64.deb ..
.
Unpacking postgresql-client-12 (12.7-1.pgdg20.10+1) ...
Selecting previously unselected package postgresql-common.
Preparing to unpack .../08-postgresql-common_226.pgdg20.10+1_all.deb ...
Adding 'diversion of /usr/bin/pg_config to /usr/bin/pg_config.libpq-dev by post
gresql-common'
Unpacking postgresql-common (226.pgdg20.10+1) ...
Selecting previously unselected package postgresql-12.
Preparing to unpack .../09-postgresql-12_12.7-1.pgdg20.10+1_amd64.deb ...
Unpacking postgresql-12 (12.7-1.pgdg20.10+1) ...
Selecting previously unselected package sysstat.
Preparing to unpack .../10-sysstat_12.4.0-1_amd64.deb ...
Unpacking sysstat (12.4.0-1) ...
Setting up pgdg-keyring (2018.2) ...
Removing apt.postgresql.org key from trusted.gpg: OK
Setting up libpq5:amd64 (13.3-1.pgdg20.10+1) ...
Setting up libcommon-sense-perl (3.75-1build2) ...
Setting up libtypes-serialiser-perl (1.0-1) ...
Setting up libjson-perl (4.02000-2) ...
Setting up sysstat (12.4.0-1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) i
n auto mode
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-collect.timer
→ /lib/systemd/system/sysstat-collect.timer.
Created symlink /etc/systemd/system/sysstat.service.wants/sysstat-summary.timer
→ /lib/systemd/system/sysstat-summary.timer.
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /
lib/systemd/system/sysstat.service.
Setting up postgresql-client-common (226.pgdg20.10+1) ...
Setting up libjson-xs-perl (4.020-1build1) ...
Setting up postgresql-client-12 (12.7-1.pgdg20.10+1) ...
update-alternatives: using /usr/share/postgresql/12/man/man1/psql.1.gz to provi
de /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode
Setting up postgresql-common (226.pgdg20.10+1) ...
Adding user postgres to group ssl-cert

Creating config file /etc/postgresql-common/createcluster.conf with new version
Building PostgreSQL dictionaries from installed myspell/hunspell packages...
en_us
Removing obsolete dictionary files:
Created symlink /etc/systemd/system/multi-user.target.wants/postgresql.service 
→ /lib/systemd/system/postgresql.service.
Setting up postgresql-12 (12.7-1.pgdg20.10+1) ...
Creating new PostgreSQL cluster 12/main ...
/usr/lib/postgresql/12/bin/initdb -D /var/lib/postgresql/12/main --auth-local p
eer --auth-host md5
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/12/main ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/New_York
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

pg_ctlcluster 12 main start

Ver Cluster Port Status Owner Data directory Log file
12 main 5432 down postgres /var/lib/postgresql/12/main /var/log/postgresq
l/postgresql-12-main.log
update-alternatives: using /usr/share/postgresql/12/man/man1/postmaster.1.gz to
provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Processing triggers for systemd (246.6-1ubuntu1) ...
Processing triggers for man-db (2.9.3-2) ...
Processing triggers for libc-bin (2.32-0ubuntu3) ...
root@PostgreSQL:~# 

Fase 5:controlla lo stato del servizio PostgreSQL

systemctl status postgresql.service 

Esempio di output:

root@PostgreSQL:~# systemctl status postgresql.service 
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor p>
Active: active (exited) since Sun 2021-05-16 09:17:56 EDT; 1min 56s ago
Main PID: 10932 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4648)
Memory: 0B
CGroup: /system.slice/postgresql.service

May 16 09:17:56 PostgreSQL systemd[1]: Starting PostgreSQL RDBMS...
May 16 09:17:56 PostgreSQL systemd[1]: Finished PostgreSQL RDBMS.

root@PostgreSQL:~#

Passaggio 6:connetti PostgreSQL 

sudo su - postgres

Passaggio 7:reimposta la password PostgreSQL

Si consiglia di reimpostare la password su una password complessa

psql -c "alter user postgres with password 'StrongAdminP@ssw0rd'"

Esempio di output:

root@PostgreSQL:~# sudo su - postgres
postgres@PostgreSQL:~$ 
postgres@PostgreSQL:~$ psql -c "alter user postgres with password 'StrongAdminP@ssw0rd'"
ALTER ROLE
postgres@PostgreSQL:~$

Passaggio 8:crea il database PostgreSQL 

$ psql

$ CREATE DATABASE firsttestdb;

$ CREATE USER testuser1 WITH ENCRYPTED PASSWORD 'MyDBP@ss0rd';

$ GRANT ALL PRIVILEGES ON DATABASE firsttestdb to testuser1;

Elenca il database PostgreSQL :

$\l

Come connettersi al database PostgreSQL:

 \c firsttestdb

Esempio di output:

postgres=# \c firsttestdb
You are now connected to database "firsttestdb" as user "postgres".
firsttestdb=#

Ora vedremo come consentire connessioni remote a PostgreSQL DB.

Fase 9:consenti la connessione remota al database PostgreSQL

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

Decommenta la riga "listen_addresses ='*'

Salva il file e riavvia il servizio PostgreSQL

sudo systemctl restart postgresql 

Fase 10:controlla l'indirizzo di ascolto

 netstat  -tunelp | grep 5432

Esempio di output:

root@PostgreSQL:~# netstat -antple | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 135 253939 13850/postgres 
tcp6 0 0 :::5432 :::* LISTEN 135 253940 13850/postgres 
root@PostgreSQL:~#

Alla fine dell'articolo, abbiamo visto come installare PostgreSQL 12 su Ubuntu 20.04/18.04/16.04.