技術ブログ

(技術系中心)基本自分用備忘録なので、あくまで参考程度でお願いします。

ポスグレの初期化で詰まった話

DBをUTF-8で初期化しようとして下記のコマンドを入力

initdb /usr/local/var/postgres -E utf8

すると下記のようなエラーに遭遇

The files belonging to this database system will be owned by user "inouehiroki".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

creating directory /usr/local/var/postgres ... initdb: error: could not create directory "/usr/local/var": Permission denied

perimissionエラーぽい。 この記事を参考にsudo権限で755(読み出せて、書き込めて、実行可能)権限を付与するようにしてみた。

すると下記のエラーに遭遇

✗ sudo mkdir /usr/local/var/postgres

mkdir: /usr/local/var: No such file or directory

そもそも/usr/local/varが存在しないということなので、/usr/local/varを作成した後に権限付与する。

sudo mkdir /usr/local/var
sudo mkdir /usr/local/var/postgres
sudo chmod 775 /usr/local/var/postgres
sudo chown ユーザー名 /usr/local/var/postgres

この後にもう一度initdb /usr/local/var/postgres -E utf8を実施すると上手くいった

➜  local initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "inouehiroki".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start