postgresqlをインストールしテーブルにデータを登録するまで(postgresql on CentOS8)

インストール

# dnf install postgresql

サーバーをインストール

# dnf install postgresql-server


初期化

# postgresql-setup initdb


設定ファイル変更
# cd /var/lib/pgsql/data/

デフォルト設定をバックアップ
# cp postgresql.conf postgresql.conf.org

# vi /var/lib/pgsql/data/postgresql.conf

最後の行に1行追加

  ....
  # CUSTOMIZED OPTIONS
  #------------------------------------------------------------------------------
  # Add settings for extensions here
  listen_addresses = '*'


もう一個設定ファイル変更

# cd ../data
# pwd
/var/lib/pgsql/data

# cp pg_hba.conf pg_hba.conf.org
# vi pg_hba.conf

以下のように変更
# cat pg_hba.conf

  # PostgreSQL Client Authentication Configuration File
  # ===================================================
 local   all     all                     trust
  host    all     all     127.0.0.1/32    trust
  host    all     all     ::1/128         trust




postgresqlを再起動

# systemctl restart postgresql

active(running) になる。

# systemctl status postgresql

● postgresql.service - PostgreSQL database server

   Loaded: loaded (/usr/lib/systemd/system/postgresql.service; disabled; vendor preset: disabled)

   Active: active (running) since Sat 2020-02-15 13:58:56 JST; 1s ago

  Process: 14332 ExecStartPre=/usr/libexec/postgresql-check-db-dir postgresql (code=exited, status=0/SUCCESS)
(以下略)


接続

# psql -U postgres -h 127.0.0.1 -w
psql (10.6)
Type "help" for help.

postgres=#

^Zで抜ける


パスワード変更

# psql -U postgres -c "ALTER ROLE postgres WITH PASSWORD 'hogehoge'"
ALTER ROLE

データベース作成


#psql -U postgres -W -c "CREATE DATABASE my_db";
Password for user postgres:

CREATE DATABASE

データベースを指定して接続

psql -U postgres my_db
my_db=#

Users というテーブルを作る

my_db=# create table Users (
my_db(#   userid         char(032)     primary key,
my_db(#   fullname       char(100),
my_db(#   phonenum       char(11),
my_db(#   postal         char(7),
my_db(#   created        timestamp,
my_db(# active boolean);
CREATE TABLE

my_db=#

確認

my_db=# \d
         List of relations
 Schema | Name  | Type  |  Owner
--------+-------+-------+----------
 public | users | table | postgres

(1 row)



テーブル定義を表示



my_db=# \d Users

                          Table "public.users"
  Column  |            Type             | Collation | Nullable | Default
----------+-----------------------------+-----------+----------+---------
 userid   | character(32)               |           | not null |
 fullname | character(100)              |           |          |
 phonenum | character(11)               |           |          |
 postal   | character(7)                |           |          |
 created  | timestamp without time zone |           |          |
 active   | boolean                     |           |          |
Indexes:
    "users_pkey" PRIMARY KEY, btree (userid)


テーブルにレコードを追加

my_db=# INSERT INTO Users(userid,fullname,created) VALUES ('john','John Lennon', current_timestamp);

INSERT 0 1

my_db=# INSERT INTO Users(userid,fullname,created) VALUES ('paul','Paul McCartney', current_timestamp);

INSERT 0 1


テーブルのレコードを表示



my_db=# select * from Users;

   userid   |   fullname      | phonenum | postal |     created                | active
------------+-----------------+----------+--------+----------------------------+--------
 John       | John Lennon     |          |        | 2020-02-15 14:23:36.832459 |
 monqy      | Paul McCartney  |          |        | 2020-02-15 14:23:53.176765 |
(2 rows)


※実際は項目長の長さだけ表示されるので折り返して汚くなったが上記は見やすく整形してある。



とりあえず一つのテーブルが作成できた。

今度はこのDBの内容をCGIで表示してみる。
そして登録、修正、削除ができるようにする。