ユーザ用ツール

サイト用ツール


サイドバー

Index

はじめてのおつかい






DokuWiki整形記法


PlayGround



serverapps:db:postgresql

PostgreSQL

インストール

# pkg install postgresql10-server

/etc/rc.conf

postgresql_enable="YES"
postgresql_data="/usr/local/pgsql/DATA"

初期化

# mkdir /usr/local/pgsql
# chown postgres:postgres /usr/local/pgsql
# su - postgres
$ initdb -D /usr/local/pgsql/data

または
# service postgresql initdb
または
# /usr/local/etc/rc.d/postgresql initdb

クライアント認証

/usr/local/pgsql/data/pg_hba.conf に設定します。 標準では自分自身からの接続のみを許可しています。

# TYPE  DATABASE        USER            CIDR-ADDRESS            METHOD
host    all             all             192.168.0.0/24          md5

これで、192.168.0.xxxからの接続が許可されます。1)

ほかからアクセスする場合には、postgresql.confのlisten_addresses も変更が必要です。

listen_addresses = '*'

起動

# service postgresql start

password変更

rootユーザのパスワードを変更します。2)

alter role postgres with password 'xxxxxxxxxxxxxxxxxxxx';

Role(ユーザ)の追加

# su - postgres
$ createuser -d -U postgres -P ユーザー名
Enter password for new role: 
Enter it again: 
Shall the new role be a superuser? (y/n) 

-d

データベースの作成を許可

-P

パスワード入力プロンプトを表示

または、psqlの中で

CREATE ROLE role_name WITH LOGIN PASSWORD 'password'

ユーザの一覧

# psql postgres

postgres=# \du
                             List of roles
 Role name | Superuser | Create role | Create DB | Connections | Member of 
-----------+-----------+-------------+-----------+-------------+-----------
 dbuser1   | no        | no          | no        | no limit    | {}
 dbuser2   | no        | no          | no        | no limit    | {}
 postgres  | yes       | yes         | yes       | no limit    | {}
(3 rows)
postgres=# 

パスワード変更

# psql postgres

postgres=# alter role postgres with password 'NewPassword';
ALTER ROLE
postgres=# 

データベースの作成

$ createdb -E UTF8 -O オーナー名 データベース名

私は、面倒なのでpgadminから作っています。;-)

SQLの場合

CREATE DATABASE [database_name]
    [ [ WITH ] [ OWNER [=] user_name ]
           [ TEMPLATE [=] template ]
           [ ENCODING [=] encoding ]
           [ LC_COLLATE [=] lc_collate ]
           [ LC_CTYPE [=] lc_ctype ]
           [ TABLESPACE [=] tablespace_name ]
           [ CONNECTION LIMIT [=] connlimit ] ]

文字コードが標準と違う場合には

TEMPLATE = template0

を指定する必要があります3)

日本語DBの作成例

CREATE DATABASE dbname WITH OWNER ownername 
  ENCODING UTF8 LC_COLLATE 'ja_JP.UTF-8' LC_CTYPE 'ja_JP.UTF-8'
  TEMPLATE template0
;

なお、localeはエンコーディング部分はUTF8ではなくてUTF-8となります。マニュアルなどにはハイフンなしで書かれていますがハイフンが必要です。4)

AutoVacuum

デフォルトの設定では、自動バキュームは有効で、関連するパラメータも適切に設定されています。 5)6)

参考

1)
md5は暗号化パスワード接続
2)
パスワードは別紙参照
3)
必須です
4)
FreeBSDで確認したので、Linuxでは違うかもしれません。マニュアルにも環境依存と書かれていますし…
5)
9.0.x
serverapps/db/postgresql.txt · 最終更新: 2023/07/28 19:52 by hayashi