Ubuntu PostgreSQL のインストール

Tech Knowledge
公開: 2024-08-20

参考

PostgreSQL を Ubuntu に普通にインストール

手順

サーバー

sudo apt install postgresql postgresql-contrib

クライアント

sudo apt install postgresql-client

エラーになった

korochin@koro-P6-I7G6:~/hoge/fuga$ sudo apt install postgresql-client
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  libpq5 postgresql-client-14
Suggested packages:
  postgresql-14 postgresql-doc-14
The following NEW packages will be installed:
  libpq5 postgresql-client postgresql-client-14
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 1366 kB of archives.
After this operation, 4397 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Ign:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libpq5 amd64 14.9-0ubuntu0.22.04.1
Ign:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 postgresql-client-14 amd64 14.9-0ubuntu0.22.04.1
Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 postgresql-client all 14+238 [3292 B]
Err:1 http://security.ubuntu.com/ubuntu jammy-updates/main amd64 libpq5 amd64 14.9-0ubuntu0.22.04.1
  404  Not Found [IP: 91.189.91.83 80]
Err:2 http://security.ubuntu.com/ubuntu jammy-updates/main amd64 postgresql-client-14 amd64 14.9-0ubuntu0.22.04.1
  404  Not Found [IP: 91.189.91.83 80]
Fetched 3292 B in 1s (2373 B/s)
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/p/postgresql-14/libpq5_14.9-0ubuntu0.22.04.1_amd64.deb  404  Not Found [IP: 91.189.91.83 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/p/postgresql-14/postgresql-client-14_14.9-0ubuntu0.22.04.1_amd64.deb  404  Not Found [IP: 91.189.91.83 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

apt-get update を実行後、再度インストールコマンドを実行するとインストールできた。

sudo apt-get update
sudo apt install postgresql
または
sudo apt install postgresql-client

バージョンの確認(サーバー)

sudo -u postgres psql
psql (14.13 (Ubuntu 14.13-0ubuntu0.22.04.1))

バージョンの確認(クライアント)

psql -V
psql (PostgreSQL) 14.13 (Ubuntu 14.13-0ubuntu0.22.04.1)

設定の変更(サーバーインストール時)

  • 設定ファイル(/etc/postgresql/14/main/postgresql.conf)の変更 外部からの接続を許可 ポート番号:15432
sudo vi /etc/postgresql/14/main/postgresql.conf

# - Connection Settings -

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
listen_addresses = '*'
port = 15432                            # (change requires restart)
  • 設定ファイル(/etc/postgresql/14/main/pg_hba.conf)の変更
sudo vi /etc/postgresql/14/main/pg_hba.conf

host    replication     all             ::1/128                 scram-sha-256

再起動

sudo systemctl restart postgresql.service

パスワードの変更

ALTER ROLE postgres WITH PASSWORD 'password';