Ubuntu22にPostgreSQL15をインストールする

Linux

Ubuntu22にPostgre15をインストールする方法について紹介します。
途中、少し手こずったので、対処方法についても紹介します。


環境


  • Ubuntu Server 22.03.3 LTS
  • PostgreSQL15.4
  • rootユーザ

PostgreSQLのバージョン確認


まず、Ubuntu22でのPostgreSQL標準バージョンを確認します。

apt info postgresql

以下のバージョンが標準バージョンとなります。

Package: postgresql
Version: 14+238

Ubuntu構築時点では認識できる標準バージョンが古い場合があるので、レポジトリを追加して、再度、PostgreSQL標準バージョンを確認します。

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
sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
cat /etc/apt/sources.list.d/pgdg.list 
↓レポジトリが追加されていることを確認
deb http://apt.postgresql.org/pub/repos/apt jammy-pgdg main
↑レポジトリが追加されていることを確認

apt update
apt info postgresql
↓標準バージョンが変化していることを確認
Package: postgresql
Version: 15+253.pgdg22.04+1
↑標準バージョンが変化していることを確認

 


PostgreSQLをインストール


とりあえずPostgreSQLをインストールしてみます。


apt install -y postgresql

以下のメッセージが表示されてエラーとなります。

E: Failed to fetch http://apt.postgresql.org/pub/repos/apt/pool/main/p/postgresql-15/postgresql-15_15.4-1.pgdg22.04%2b1_amd64.deb  Connection failed [IP: 87.238.57.227 80]		
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?		

“apt-get update”をやってみろと言われているので、素直にやってみます。
依存関係の更新ですね。

apt-get update
apt-get install
apt install -y postgresql

しかし、同じエラーとなってしまいました。

E: Failed to fetch http://apt.postgresql.org/pub/repos/apt/pool/main/p/postgresql-15/postgresql-15_15.4-1.pgdg22.04%2b1_amd64.deb  Connection failed [IP: 147.75.85.69 80]	
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?	

パッケージを手動ダウンロードして再インストール


パッケージのダウンロードでエラーとなっているようなので、手動でダウンロードしてみます。
手動でフォルダを作成し、そのフォルダにダウンロードします。

mkdir -p /tmp/
cd /tmp/
wget http://apt.postgresql.org/pub/repos/apt/pool/main/p/postgresql-15/postgresql-15_15.4-1.pgdg22.04%2b1_amd64.deb
ls -ltr
↓パッケージがダウンロードされていることを確認
postgresql-15_15.4-1.pgdg22.04+1_amd64.deb
↑パッケージがダウンロードされていることを確認

ダウンロードできたかことが確認できたら、ダウンロードしたパッケージのみをインストールします。


dpkg -i ./postgresql-15_15.4-1.pgdg22.04+1_amd64.deb

再度、インストールを実施してみます。

apt install -y postgresql

以下のエラーになってしまいます。

E: Unmet dependencies. Try 'apt --fix-broken install' with no packages (or specify a solution).

“–fix-broken”オプションを付けて依存関係を更新しろといわれているのでやってみます。

apt --fix-broken install

再度インストールをやってみます。

apt install -y postgresql

正常にインストールが終了したら、「systemctl」で起動していることを確認して終了です。

root@zst:~# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor pres
     Active: active (exited) since Sun 2023-09-03 11:07:21 UTC; 7s ago
    Process: 68255 ExecStart=/bin/true (code=exited, status=0/SUCCESS)
   Main PID: 68255 (code=exited, status=0/SUCCESS)
        CPU: 3ms
Sep 03 11:07:21 zst systemd[1]: Starting PostgreSQL RDBMS...
Sep 03 11:07:21 zst systemd[1]: Finished PostgreSQL RDBMS.