sql >> Database >  >> RDS >> PostgreSQL

Hoe PostgreSQL 12 op Ubuntu 20.04/18.04/16.04 te installeren?

In dit artikel laten we u zien hoe u PostgreSQL 12 installeert op Ubuntu 20.04/18.04/16.04. PostgreSQL is een Open-source databasesysteem en het is net zo krachtig als andere relationele databasesystemen.

Je kunt de releasepagina van postgreSQL 12 bekijken. 

Stappen om PostgreSQL 12 op Ubuntu te installeren 

Stap 1:Update Ubuntu-systeem

Het wordt altijd aanbevolen om het systeem bij te werken voordat u PostgreSQL installeert.

sudo apt update

Stap 2:Installeer het vereiste pakket 

sudo apt -y install vim bash-completion wget

Als het pakket al is geïnstalleerd, krijgt u onderstaande uitvoer, u kunt het negeren.

Voorbeelduitvoer:

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:~# 

Stap 3:PostgreSQL 12-repository toevoegen

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

Stap 4:installeer PostgreSQL 12 op Ubuntu

sudo apt update

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

Voorbeelduitvoer:

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:~# 

Stap 5:Controleer de PostgreSQL-servicestatus

systemctl status postgresql.service 

Voorbeelduitvoer:

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:~#
voltooid

Stap 6:PostgreSQL verbinden 

sudo su - postgres

Stap 7:Reset het PostgreSQL-wachtwoord

Het wordt aanbevolen om het wachtwoord opnieuw in te stellen op een sterk wachtwoord

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

Voorbeelduitvoer:

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

Stap 8:PostgreSQL-database maken 

$ psql

$ CREATE DATABASE firsttestdb;

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

$ GRANT ALL PRIVILEGES ON DATABASE firsttestdb to testuser1;

Maak een lijst van de PostgreSQL-database:

$\l

Hoe verbinding te maken met de PostgreSQL-database:

 \c firsttestdb

Voorbeelduitvoer:

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

Nu zullen we zien hoe u externe verbindingen met PostgreSQL DB kunt toestaan.

Stap 9:externe verbinding met PostgreSQL-database toestaan

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

Uncomment regel "listen_addresses ='*'

Sla het bestand op en start de PostgreSQL-service opnieuw

sudo systemctl restart postgresql 

Stap 10:Controleer het luisteradres

 netstat  -tunelp | grep 5432

Voorbeelduitvoer:

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:~#

Aan het einde van het artikel hebben we gezien hoe u PostgreSQL 12 installeert op Ubuntu 20.04/18.04/16.04.


  1. Toegang geweigerd voor gebruiker '[email protected]' (met wachtwoord:NO)

  2. BadImageFormatException. Dit gebeurt bij het uitvoeren in 64-bits modus met de 32-bits Oracle-clientcomponenten geïnstalleerd

  3. Hoe de prestaties van Moodle te benchmarken?

  4. toegang krijgen tot een kolomaliassen in de waar-clausule in postgresql