金子邦彦研究室インストールUbuntu, WSL2PostgreSQL 14, pgAdmin 4, PostGIS 3 のインストール(Ubuntu 上)

PostgreSQL 14, pgAdmin 4, PostGIS 3 のインストール(Ubuntu 上)

Ubuntu に,PostgreSQL 14, pgAdmin 4, PostGIS 3 のインストールを行う. そして,pgAdmin 4 の主要機能を動かしてみる.

※ インストールは,https://wiki.postgresql.org/wiki/Apt に記載の手順に従う.

目次

  1. 前準備
  2. PostgreSQL 14, pgAdmin 4, PostGIS 3 のインストール(Ubuntu 上)
  3. peer 認証の確認と md5 認証の設定
  4. PostgreSQL データベースサーバの起動と終了
  5. psql の基本操作
  6. テーブル定義とレコード挿入
  7. pgAdmin4 のセットアップ

関連する外部ページ

前準備

Ubuntu のシステム更新

UbuntuUbuntu で OS のシステム更新を行うときは, 次のコマンドを実行.

UbuntuUbuntu のインストールは別ページ »で説明

sudo apt -y update
sudo apt -yV upgrade
sudo /sbin/shutdown -r now

PostgreSQL 14, pgAdmin 4, PostGIS 3 のインストール(Ubuntu 上)

https://wiki.postgresql.org/wiki/Apt に記載の手順に従う.

  1. レポジトリキーのインストール

    sudo apt -y update
    sudo apt -y install curl ca-certificates gnupg
    curl https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo apt-key add
    curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
    

    エラーメッセージが出ないことを確認.

    [image]
  2. パッケージリストの取得
    sudo sh -c 'echo "deb https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list'
    sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
    

    [image]
  3. パッケージ情報の更新
    sudo apt -y update
    

    [image]
  4. インストール
    apt-cache search postgresql
    sudo apt -y install pgadmin4 postgresql-14 postgresql-14-postgis-3
    

    [image]
  5. インストール直後なので,確認のため,サーバを手動で起動して停止してみて,エラーメッセージが出ないことを確認する

    sudo pg_ctlcluster 14 main restart
    sudo pg_ctlcluster 14 main status
    

    [image]
  6. PostgreSQL の動作確認

    Ubuntu のサービスアカウント postgres と peer 認証により,PostgreSQL の psql を使ってみる.

    「\c」により,使用されている PostgreSQL のロール名と,オープンされているデータベース名を確認.

    確認したら,「\q」により終了.

    sudo -u postgres psql
    \c
    \q
    

    [image]
  7. データベースの確認

    postgres, template0, template1 の 3 つのデータベースが表示されることを確認.

    sudo -u postgres psql
    \l
    \q
    

    [image]
  8. psql のバージョン確認
    psql --version
    

    [image]

peer 認証の確認と md5 認証の設定

peer 認証について確認する.そして,md5 認証について設定できることを確認する.

peer 認証の確認

  1. PostgreSQL サービスアカウント (service account)の確認

    PostgreSQL サービスアカウントは,Postgres サーバの起動等に使うもの.Ubuntu では,既定(デフォルト)で,peer 認証が行えるようにも設定されている.

    Ubuntu でパッケージを使って PostgreSQL をインストールすると,PostgreSQL サービスアカウント(ユーザ名は「postgres」)が自動的に作成される.

    次のコマンドで確認できる.

    sudo cat /etc/passwd | grep postgres
    

    ※ 「x」は no password という意味(パスワードがないという意味ではない)

    [image]

    PostgreSQL サービスアカウントは Linux が管理するアカウントのこと.PostgreSQL が管理するロールとは別のものである.

  2. PostgreSQL の認証は peer 認証が設定されていることを確認

    sudo cat /etc/postgresql/14/main/pg_hba.conf
    

    [image]
  3. Ubuntu のサービスアカウント postgres と peer 認証により,PostgreSQL の psql を使ってみる.

    「\c」により,使用されている PostgreSQL のロール名と,オープンされているデータベース名を確認.

    確認したら,「\q」により終了.

    sudo -u postgres psql
    \c
    \q
    

    [image]

md5 認証の設定

PostgreSQL での新しいロールの追加と md5 認証の設定を行ってみる.

これは,「Redmine のインストール」に必要な設定ではない. PostgreSQL での新しいロールの追加と md5 認証の設定を試したいときのためのもの.

下の手順で,PostgreSQL で,新しいロール testuser を作成し md5 認証できるように設定してみる

  1. PostgreSQL で,新しいロール testuser を作成

    パスワードは,下のものをそのまま使うのでなく,必ず独自に設定してください.

    sudo -u postgres psql
    create role "testuser" with login encrypted password 'hoge7618mhty';
    \du
    \q
    

    [image]
  2. /etc/postgresql/14/main/pg_hba.conf を書き換えて,全ユーザである all の md5 認証を有効にする.

    そのために,次の1行を追加する

    同時に「local all postgres peer」の行の頭に「#」を付ける

    local   all             all                                md5
    

    [image]
  3. PostgreSQL サーバの再起動

    エラーメッセージが出ていなければ OK.

    sudo pg_ctlcluster 14 main restart
    sudo pg_ctlcluster 14 main status
    

    [image]
  4. 新しいロール testuser で,md5 認証により psql を使用できるか確認する.

    md5 認証のときは psql -U <ロール名> -d <データベース名>

    psql -U testuser -d postgres
    \c
    \q
    

    [image]

PostgreSQL データベースサーバの起動と終了

PostgreSQL データベースサーバの再起動

sudo pg_ctlcluster 14 main restart
sudo pg_ctlcluster 14 main status

[image]

PostgreSQL データベースサーバの終了

sudo pg_ctlcluster 14 main stop
sudo pg_ctlcluster 14 main status

[image]

psql の基本操作

psql の種々の操作について: 別ページ »で説明

テーブル定義とレコード挿入

  1. 「PostgreSQL データベース管理者のユーザ名」で,psql を用いて接続.

    sudo -u postgres psql
    

    [image]
  2. SQL を用いたテーブル定義

    create table order_records (
        id            integer primary key not null,
        year          integer not null CHECK ( year > 2008 ),
        month         integer not null CHECK ( month >= 1 AND month <= 12 ),
        day           integer not null CHECK ( day >= 1 AND day <= 31 ),
        customer_name text not null,
        product_name  text not null,
        unit_price    real not null check ( unit_price > 0 ),
        qty           integer not null default 1 check ( qty > 0 ),
        created_at    timestamp with time zone not null default current_timestamp,
        updated_at    timestamp with time zone not null default current_timestamp,
        check ( ( unit_price * qty ) < 200000 ) );
    

    [image]
  3. SQL を用いたレコード挿入

    begin transaction;
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 1, 2022, 7, 26,  'kaneko', 'orange A', 1.2, 10 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 2, 2022, 7, 26,  'miyamoto', 'Apple M',  2.5, 2 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price, qty) values( 3, 2022, 7, 27,  'kaneko',   'orange B', 1.2, 8 );
    insert into order_records (id, year, month, day, customer_name, product_name, unit_price) values( 4, 2022, 7, 28,  'miyamoto',   'Apple L', 3 );
    commit;
    

    [image]
  4. 確認表示

    select * from order_records;
    

    [image]
  5. 更新し確認表示

    begin transaction;
    update order_records set unit_price = 11.2 where id = 1;
    commit;
    select * from order_records;
    

    [image]

    [image]
  6. テーブル一覧の表示
    \d
    

    [image]
  7. psql の終了
    \q
    

    [image]

pgAdmin4 のセットアップと使用

  1. メールアドレス,パスワードの設定
    sudo /usr/pgadmin4/bin/setup-web.sh
    
  2. Web ブラウザで http://127.0.0.1/pgadmin4 を開く