openSUSE Leap 16

PostgreSQL 17 : インストール2025/11/25

 

PostgreSQL をインストールし、データベースサーバーを構築します。

[1] PostgreSQL をインストールして起動します。
www:~ #
zypper -n install postgresql17-server
www:~ #
systemctl enable --now postgresql

[2] デフォルト設定では、ローカルホストからのみ接続可能 且つ ローカル接続は [peer] 認証のみとなっています。 認証方式の詳細は公式ドキュメントを参照ください。
⇒ https://www.postgresql.org/docs/16/auth-pg-hba-conf.html
# ローカルホストをリスン

www:~ #
grep listen_addresses /var/lib/pgsql/data/postgresql.conf

#listen_addresses = 'localhost' # what IP address(es) to listen on;
# 認証方式

www:~ #
grep -v -E "^#|^$" /var/lib/pgsql/data/pg_hba.conf

local   all             all                                     peer
host    all             all             127.0.0.1/32            ident
host    all             all             ::1/128                 ident
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            ident
host    replication     all             ::1/128                 ident
[3] [peer] 認証の場合、任意の PostgreSQL ユーザーを追加して利用するには、同名の OS ユーザーも必要になります。
# OS ユーザー追加

www:~ #
useradd suse
# PostgreSQL 管理ユーザーで PostgreSQL ユーザーとデータベース追加

www:~ #
su - postgres

postgres@www:~>
createuser suse

postgres@www:~>
createdb testdb -O suse
# 確認

postgres@www:~>
psql -c "select usename from pg_user;"
 usename
----------
 postgres
 suse
(2 rows)

postgres@www:~>
psql -l
                                                     List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           |
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           | =c/postgres          +
           |          |          |                 |             |             |        |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           | =c/postgres          +
           |          |          |                 |             |             |        |           | postgres=CTc/postgres
 testdb    | suse     | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           |
(4 rows)
[4] 新規追加したユーザーで PostgreSQL を利用する場合の基本操作です。
# テスト DB に接続

suse@www:~>
psql testdb

psql (17.6)
Type "help" for help.

# ユーザーロール一覧を表示
testdb=> \du
                             List of roles
 Role name |                         Attributes
-----------+------------------------------------------------------------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS
 suse      |

# データベース一覧を表示
testdb=> \l
                                                     List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           |
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           | =c/postgres          +
           |          |          |                 |             |             |        |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           | =c/postgres          +
           |          |          |                 |             |             |        |           | postgres=CTc/postgres
 testdb    | suse     | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           |
(4 rows)

# テストテーブルを作成
testdb=> create table test_table (no int, name text); 
CREATE TABLE

# テーブル一覧を表示
testdb=> \dt 
          List of relations
 Schema |    Name    | Type  | Owner
--------+------------+-------+-------
 public | test_table | table | suse
(1 row)

# テストテーブルにテストデータを挿入
testdb=> insert into test_table (no,name) values (01,'SUSE'); 
INSERT 0 1

# 確認
testdb=> select * from test_table; 
 no | name
----+------
  1 | SUSE
(1 row)

# テストテーブルを削除
testdb=> drop table test_table; 
DROP TABLE

testdb=> \dt 
Did not find any relations.

# exit する
testdb=> \q 

# テストデータベースを削除

suse@www:~>
dropdb testdb

suse@www:~>
psql -l

                                                     List of databases
   Name    |  Owner   | Encoding | Locale Provider |   Collate   |    Ctype    | Locale | ICU Rules |   Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
 postgres  | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           |
 template0 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           | =c/postgres          +
           |          |          |                 |             |             |        |           | postgres=CTc/postgres
 template1 | postgres | UTF8     | libc            | en_US.UTF-8 | en_US.UTF-8 |        |           | =c/postgres          +
           |          |          |                 |             |             |        |           | postgres=CTc/postgres
(3 rows)
関連コンテンツ