LoginSignup
38
34

More than 3 years have passed since last update.

MacOS CatalinaでPostgresqlをインストール・初期操作

Last updated at Posted at 2019-10-21

導入

卒業制作の関係でWEBアプリを制作しており、レンタルサーバーでsqliteが使えないことが判明しPostgresqlをインストールしようとしたら記事がバラバラで死ぬほど時間がかかったので備忘録。

注意

当チャートは試行錯誤の上にインストールが完了し、最終的にこの流れが正しかったのだろうという振り返りの元、書き込んでいます。環境依存の問題や順番の間違いなど、当方で確認が取れ次第修正はしますが、全て自己責任の元で行ってください。

もし、記事内に修正箇所を発見された方はコメントで教えていただけると幸いです。

環境

MacOS Catallina 10.15
Homebrew 2.1.5
postgresql 11.5

インストール

ここではHomebrewを使ってインストールを進めていきます。
まだインストールしていなければ以下のリンクを参考にしてください。

Postgresqlのインストール

$ brew install postgresql

その後、

$ brew link postgresql

を、したいがおそらくこの辺りで次のようなエラーコードが出て困ってるはず。

Linking /usr/local/Cellar/postgresql/11.5_1... 
Error: Could not symlink include/ecpg_config.h
/usr/local/include is not writable.

これは/usr/localに書き込み権限が無いため。
となれば書き込み権限を与えればよい。※1

$ sudo chown -R $(whoami) /usr/local

sudoなのでパスワードを入力して実行。
もう一度、linkを行う。

$ brew link postgresql
Linking /usr/local/Cellar/postgresql/11.5_1... 392 symlinks created

link成功。
その後Postgresqlをpostinstallする。

$ brew postinstall postgresql
==> Postinstalling postgresql
==> /usr/local/Cellar/postgresql/11.5_1/bin/initdb --locale=C -E UTF-8 /usr/local

これでPostgresqlのインストールは完了。
最後にデータベースをutf-8で初期化する。

データベースの初期化

$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "(ここにユーザーネーム)".
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.

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

なにやらエラーコードが出てるがこれで処理は完了しているらしい。
本当に初期化できてるのか、起動して確認する。

起動

$ brew services start postgresql

バージョン確認

$ psql -V
psql (PostgreSQL) 11.5

この時、psqlコマンドが見つからないとエラーが出た場合、
PostgresqlにPATHが通ってない証拠なので以下のリンクを参考に通す。※2

データベース一覧を確認

$ psql -l
                                 List of databases
   Name    |    Owner    | Encoding | Collate | Ctype |      Access privileges      
-----------+-------------+----------+---------+-------+-----------------------------
 postgres  | ユーザーネーム | UTF8     | C       | C     | 
 template0 | ユーザーネーム | UTF8     | C       | C     | =c/ユーザーネーム             +
           |             |          |         |       | ユーザーネーム=CTc/ユーザーネーム
 template1 | ユーザーネーム | UTF8     | C       | C     | =c/ユーザーネーム             +
           |             |          |         |       | ユーザーネーム=CTc/ユーザーネーム
(3 rows)

これが出て、utf-8でエンコードされていれば初期化は成功。
インストール完了となる。

注意点

※1

他のブログなどでは書いてなかったのだが、私の環境ではsudoなのに、このユーザー権限を与える段階あたりでうまく権限が与えられずに困ったことがあった。
調べるとSIPと呼ばれるカーネルセキュリティで保護されているからとのこと。

上記のリンクを参考にSIPを解除したところ問題なくステップを進めることができた。
もしsudoコマンドで困っている場合は試してほしい。

※2

これも他のブログでは全くといっていいほど説明されてないので、もしかすると環境依存なのかもしれない。おそらく本来ならlinkコマンドが通った時点で追加されるのかもしれないが、当環境では手動でインストールせざるを得なかった。

ちなみに、「psql path」と検索するとこちらのQiitaが一番上に出るが、

こちらの環境ではPostgresqlのインストール場所が、
/usr/local/pgsql/bin/
となっているが、Homebrewインストールの場合はここにはない。
/usr/local/cellar/postgresql/11.5_1/bin
となるので注意。

38
34
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
34