PostgreSQLインストール、Ubuntu 20.04

Ubuntu 20.04に、PostgreSQLを入れる覚え書きです。

PostgreSQL サポートバージョン

PostgreSQL: Versioning Policy

現時点(2022年6月)で、サポートが受けられるバージョンは、
PostgreSQL 10.xx
PostgreSQL 11.xx
PostgreSQL 12.xx
PostgreSQL 13.xx
PostgreSQL 14.xx
です。

PostgreSQL 10.xx は、
今年(2022年)の11月でサポートが終了します。

ubuntu 20.04に標準で入る、PostgreSQLは?

標準で入る、PostgreSQLをインストールしてみます。

# apt install postgresql
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 linux-headers-5.4.0-66 linux-headers-5.4.0-66-generic linux-image-5.4.0-66-generic linux-modules-5.4.0-66-generic linux-modules-extra-5.4.0-66-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libllvm10 libpq5 libsensors-config libsensors5 postgresql-12 postgresql-client-12 postgresql-client-common postgresql-common ssl-cert sysstat
Suggested packages:
  lm-sensors postgresql-doc postgresql-doc-12 libjson-perl openssl-blacklist isag
The following NEW packages will be installed:
  libllvm10 libpq5 libsensors-config libsensors5 postgresql postgresql-12 postgresql-client-12 postgresql-client-common postgresql-common ssl-cert sysstat
0 upgraded, 11 newly installed, 0 to remove and 0 not upgraded.
Need to get 30.7 MB of archives.
After this operation, 122 MB of additional disk space will be used.
Do you want to continue? [Y/n] 

(略)

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/postgresql/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
Setting up sysstat (12.2.0-2ubuntu0.1) ...

Creating config file /etc/default/sysstat with new version
update-alternatives: using /usr/bin/sar.sysstat to provide /usr/bin/sar (sar) in auto mode
Created symlink /etc/systemd/system/multi-user.target.wants/sysstat.service → /lib/systemd/system/sysstat.service.
Setting up postgresql (12+214ubuntu0.1) ...
Processing triggers for systemd (245.4-4ubuntu3.17) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

↑インストール出来ました。

# pg_ctlcluster 12 main start
# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
     Active: active (exited) since Wed 2022-XX-XX XX:XX:XX JST; 2min 25s ago
   Main PID: 2629 (code=exited, status=0/SUCCESS)
      Tasks: 0 (limit: 2273)
     Memory: 0B
     CGroup: /system.slice/postgresql.service

systemd[1]: Starting PostgreSQL RDBMS...
systemd[1]: Finished PostgreSQL RDBMS.

↑起動をしました。

# su - postgres

$ psql

postgres=# SELECT version();
                                                                version                                                                 
----------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.11 (Ubuntu 12.11-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)

↑PostgreSQL 12.11が入りました。

PostgreSQLの最新版(PostgreSQL 14)をインストール

現時点での最新版をインストールしたいと思います。

Apt - PostgreSQL wiki

↑公式サイトでの手順書が参考になります。

# apt install curl ca-certificates gnupg
Reading package lists... Done
Building dependency tree       
Reading state information... Done
ca-certificates is already the newest version (20210119~20.04.2).
ca-certificates set to manually installed.
curl is already the newest version (7.68.0-1ubuntu2.11).
curl set to manually installed.
gnupg is already the newest version (2.2.19-3ubuntu2.1).
gnupg set to manually installed.
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 linux-headers-5.4.0-66 linux-headers-5.4.0-66-generic linux-image-5.4.0-66-generic linux-modules-5.4.0-66-generic
  linux-modules-extra-5.4.0-66-generic
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
# curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null

# sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# apt update
Hit:1 http://jp.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://jp.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://jp.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]         
Get:4 http://jp.archive.ubuntu.com/ubuntu focal-security InRelease [114 kB]          
Get:5 http://apt.postgresql.org/pub/repos/apt focal-pgdg InRelease [91.7 kB]         
Get:6 http://jp.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1,815 kB]
Get:7 http://jp.archive.ubuntu.com/ubuntu focal-updates/main Translation-en [333 kB]
Get:8 http://jp.archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [998 kB]      
Get:9 http://jp.archive.ubuntu.com/ubuntu focal-updates/restricted Translation-en [142 kB]
Get:10 http://jp.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [924 kB]        
Get:11 http://jp.archive.ubuntu.com/ubuntu focal-security/main amd64 Packages [1,471 kB]   
Get:12 http://jp.archive.ubuntu.com/ubuntu focal-security/main Translation-en [252 kB]      
Get:13 http://jp.archive.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [934 kB]
Get:14 http://jp.archive.ubuntu.com/ubuntu focal-security/restricted Translation-en [133 kB]
Get:15 http://apt.postgresql.org/pub/repos/apt focal-pgdg/main amd64 Packages [239 kB]
Fetched 7,670 kB in 3s (2,895 kB/s)                              
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.

↑インストール準備が出来ました。

# apt install postgresql-14
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  libfwupdplugin1 linux-headers-5.4.0-66 linux-headers-5.4.0-66-generic linux-image-5.4.0-66-generic linux-modules-5.4.0-66-generic
  linux-modules-extra-5.4.0-66-generic
Use 'apt autoremove' to remove them.
The following additional packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm10 libpq5 libsensors-config libsensors5 libtypes-serialiser-perl pgdg-keyring postgresql-client-14
  postgresql-client-common postgresql-common ssl-cert sysstat
Suggested packages:
  lm-sensors postgresql-doc-14 openssl-blacklist isag
The following NEW packages will be installed:
  libcommon-sense-perl libjson-perl libjson-xs-perl libllvm10 libpq5 libsensors-config libsensors5 libtypes-serialiser-perl pgdg-keyring postgresql-14
  postgresql-client-14 postgresql-client-common postgresql-common ssl-cert sysstat
0 upgraded, 15 newly installed, 0 to remove and 0 not upgraded.
Need to get 33.9 MB of archives.
After this operation, 137 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

(略)

fixing permissions on existing directory /var/lib/postgresql/14/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 ... Japan
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
update-alternatives: using /usr/share/postgresql/14/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
Processing triggers for systemd (245.4-4ubuntu3.17) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.9) ...

↑PostgreSQL14(この時点での最新版)が入りました。

バージョンチェック(バージョン確認)

PostgreSQLのバージョンを確認してみます。

# su - postgres
$ psql -l
                                  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
(3 rows)

$ psql
psql (14.3 (Ubuntu 14.3-1.pgdg20.04+1))
Type "help" for help.

postgres=# SELECT version();
                                                              version                                                              
-----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 14.3 (Ubuntu 14.3-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)

↑PostgreSQL 14.3 が入っています。

タイトルとURLをコピーしました