Ubuntu Server 22.04 LTS PostgreSQL インストール

Ubuntu

PostgreSQLはオープンソースのORDBMS(オブジェクトリレーショナルデータベース管理システム)です。
オブジェクト指向データベースなのでテーブルを継承して作成することができます。
様々なデータ型(幾何データ型・ネットワークアドレス型など)をサポートしていて、カスタム関数の設定も可能です。

インストール

他のパッケージと同じでaptでインストールできます。

$ sudo apt update
$ sudo apt install postgresql postgresql-contrib

必要ならpostgresql-contribパッケージでユーティリティと機能の追加ができます。

最新版をインストール

PPAを使って公開されたパッケージを利用して最新版をインストールします。
PPAはUbuntu公式に承認されたものではないので自己責任で導入してください。
ただし、Postgresql公式のリポジトリなので問題はないと思います。

インストールされるバージョンの確認

apt list <パッケージ名>

$ sudo apt list postgresql
Listing... Done
postgresql/jammy 14+238 all

パッケージの詳細を確認したければ
apt show <パッケージ名>

$ sudo apt show postgresql
Package: postgresql
Version: 14+238
Priority: optional
Section: database
Source: postgresql-common (238)
Origin: Ubuntu
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Original-Maintainer: Debian PostgreSQL Maintainers <team+postgresql@tracker.debian.org>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 71.7 kB
Depends: postgresql-14
Suggests: postgresql-doc
Task: postgresql-server
Download-Size: 3,288 B
APT-Sources: http://jp.archive.ubuntu.com/ubuntu jammy/main amd64 Packages
Description: object-relational SQL database (supported version)
 This metapackage always depends on the currently supported PostgreSQL
 database server version.
 .
 PostgreSQL is a fully featured object-relational database management
 system.  It supports a large part of the SQL standard and is designed
 to be extensible by users in many aspects.  Some of the features are:
 ACID transactions, foreign keys, views, sequences, subqueries,
 triggers, user-defined types and functions, outer joins, multiversion
 concurrency control.  Graphical user interfaces and bindings for many
 programming languages are available as well.

バージョンが14+238だとわかりました。

リポジトリの追加

 CA証明書と暗号化ツールのインストール

$ sudo apt install curl ca-certificates gnupg

認証キーの追加

$ curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null

/etc/apt/sources.list.d/pgdg.listにリポジトリへの追加を記載

$ sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ jammy-pgdg main" >> /etc/apt/sources.list.d/postgresql.list'

パッケージ一覧を更新

$ sudo apt update
Hit:1 http://jp.archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://apt.postgresql.org/pub/repos/apt jammy-pgdg InRelease [116 kB]
Hit:3 http://jp.archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://jp.archive.ubuntu.com/ubuntu jammy-backports InRelease
Hit:5 http://jp.archive.ubuntu.com/ubuntu jammy-security InRelease
Get:6 http://apt.postgresql.org/pub/repos/apt jammy-pgdg/main amd64 Packages [258 kB]
Fetched 374 kB in 3s (109 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
All packages are up to date.

インストールできるバージョン確認

$ sudo apt list postgresql
Listing... Done
postgresql/jammy-pgdg 15+248.pgdg22.04+1 all
N: There is 1 additional version. Please use the '-a' switch to see it

「追加バージョンが1つあります。「-a」スイッチを使用して表示してください」
バージョン15+248.pgdg22.04+1が追加されてます。

$ sudo apt list -a postgresql
Listing... Done
postgresql/jammy-pgdg 15+248.pgdg22.04+1 all
postgresql/jammy 14+238 all

postgresql-contribも確認します。

$ sudo apt list -a postgresql-contrib
Listing... Done
postgresql-contrib/jammy-pgdg 15+248.pgdg22.04+1 all
postgresql-contrib/jammy 14+238 all

インストール

リポジトリを追加した以外は通常のインストールと同じです。

$ sudo apt install postgresql postgresql-contrib

バージョン確認

パッケージのバージョンを確認

インストールしているパッケージのバージョンを確認します。
複数のバージョンをインストールしている場合は起動しているバージョンでは無いので気を付けてください。
apt list –installed postgresql

$ apt list --installed postgresql
Listing... Done
postgresql/jammy-pgdg,now 15+248.pgdg22.04+1 all [installed]
N: There is 1 additional version. Please use the '-a' switch to see it

「-a」オプションでインストールされていないものも表示します。

$ apt list -a --installed postgresql
Listing... Done
postgresql/jammy-pgdg,now 15+248.pgdg22.04+1 all [installed]
postgresql/jammy 14+238 all

*(ワイルドカード)を付けて一覧をみます。
一緒にインストールされたものは[automatic]が付いています。

$ apt list --installed postgresql*
Listing... Done
postgresql-15/jammy-pgdg,now 15.2-1.pgdg22.04+1 amd64 [installed,automatic]
postgresql-client-15/jammy-pgdg,now 15.2-1.pgdg22.04+1 amd64 [installed,automatic]
postgresql-client-common/jammy-pgdg,now 248.pgdg22.04+1 all [installed,automatic]
postgresql-common/jammy-pgdg,now 248.pgdg22.04+1 all [installed,automatic]
postgresql-contrib/jammy-pgdg,now 15+248.pgdg22.04+1 all [installed]
postgresql/jammy-pgdg,now 15+248.pgdg22.04+1 all [installed]

起動中のバージョン確認

ログインユーザーをpostgresに変更します。

$ sudo -u postgres -i

psql(PostgreSQL対話的ターミナル)コマンドで確認します。

$ psql --command="select version()"
                                                              version
-----------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 15.2 (Ubuntu 15.2-1.pgdg22.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0, 64-bit
(1 row)

参考サイト

Ubuntu 20.04にPostgreSQLをインストールする方法 [クイックスタート] | DigitalOcean
UbuntuUpdates – PPA: Postgresql
How to Check the PostgreSQL Database Version

コメント

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