MacでPostgreSQLをサッとインストールして立ち上げる

PCアザラシ IT

開発中のアプリを軽く検証するため、ローカルMacでPostgreSQL環境を立ち上げた。

前提

Homebrewがインストールされていること。

Homebrew
The Missing Package Manager for macOS (or Linux).

PostgreSQLのインストール

まずはsearch。

% brew search postgresql
==> Formulae
postgresql                        postgresql@10                     postgresql@11                    postgresql@9.4                    postgresql@9.5                    postgresql@9.6

Auroraで使うバージョンに合わせて、@11をインストール。

% brew install postgresql@11

どうでもいい情報が流れてゆく合間に、けっこう重要な情報もドンブラコと流れてくる。見過ごさずに拾っておく。

To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

This formula has created a default database cluster with:
  initdb --locale=C -E UTF-8 /usr/local/var/postgres
For more details, read:
  https://www.postgresql.org/docs/11/app-initdb.html

postgresql@11 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have postgresql@11 first in your PATH run:
  echo 'export PATH="/usr/local/opt/postgresql@11/bin:$PATH"' >> ~/.zshrc

For compilers to find postgresql@11 you may need to set:
  export LDFLAGS="-L/usr/local/opt/postgresql@11/lib"
  export CPPFLAGS="-I/usr/local/opt/postgresql@11/include"


To have launchd start postgresql@11 now and restart at login:
  brew services start postgresql@11
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgresql@11 start

PostgreSQLサーバの設定

常時起動するようにサービス登録

先程のドンブラコ情報に従って、PostgreSQLサーバをサービスとして登録する。
Mac起動時に自動的にサーバプロセスを起動してくれるようになる。

% brew services start postgresql@11
==> Successfully started `postgresql@11` (label: homebrew.mxcl.postgresql@11)

PostgreSQLクライアントの設定

psqlにPATHを通す

これもドンブラコのアドバイスに従って実施。psqlコマンドはあまり使わないからPATHを通さなくてもよいかもしれないが、もし使いたくなった時に探すのも面倒なのでやっておく。環境によってzshではないので注意。

% echo 'export PATH="/usr/local/opt/postgresql@11/bin:$PATH"' >> ~/.zshrc

psqlコマンドが認識されるようになったことを確認する。

% source ~/.zshrc
% which psql
/usr/local/opt/postgresql@11/bin/psql

postgresロールの作成

psqlからサーバに繋いでみる。

% psql postgres
psql (11.9)
Type "help" for help.

postgres=#

適当な名前で(今回は「postgres」で)ユーザを作成する。

postgres=# create user postgres SUPERUSER;
CREATE ROLE

ここまでの作業で、psqlコマンド以外からも作成したユーザでパスワードなしでログインできるようになっている。あとは好きなツールでデータベースやスキーマなど作成すればよい。

〜〜〜〜〜〜 完 〜〜〜〜〜〜

余談

DBやスキーマの作成をpsqlで行う

作成したユーザに切り替える

psqlコマンドが起動中であれば、先ほど作成したユーザに切り替える。

postgres=# \c - postgres
You are now connected to database "postgres" as user "postgres".

もしpsqlを閉じてしまっている場合は、先ほど作成したユーザでpsqlに入り直す。

% psql -U postgres postgres

データベース作成

データベース一覧を表示する。

postgres=# \l

適当な名前でデータベースを作成する。

postgres=# create database test_db;
CREATE DATABASE

作成したデータベースに切り替える。

postgres=# \c test_db
You are now connected to database "test_db" as user "postgres".

スキーマ作成

スキーマを作成する。

test_db=# create schema test_schema;
CREATE SCHEMA

カレントスキーマの切り替え

カレントスキーマの確認

test_db=# SELECT current_schema();
 current_schema
----------------
 public
(1 row)

test_db=# show search_path;
   search_path
-----------------
 "$user", public
(1 row)

カレントスキーマの切り替え

test_db=# SET search_path to test_schema;
SET
test_db=# SELECT current_schema();
 current_schema
----------------
 test_schema
(1 row)

test_db=# show search_path;
 search_path
-------------
 test_schema
(1 row)

psqlの終了

postgres=# \q
%

PostgreSQLのサービスを制御する

サービス一覧

サービスの状況を確認する時はlist。

% brew services list
Name          Status  User Plist
postgresql@11 started usr1 /Users/usr1/Library/LaunchAgents/homebrew.mxcl.postgresql@11.plist

サービス停止

サービスを止めたくなった場合はstopする。

% brew services stop postgresql@11

サービス停止後の表示について注意点

一度サービスに登録すると、stop後もlistに表示が残ってしまう。

% brew services list
Name          Status  User Plist
postgresql@11 stopped

なんだか気持ち悪いが、これを消すには一度PostgreSQLをアンインストールするしか無いようだ。

How to remove service from brew services?
Can’t figure out removing a inactive service from brew services. brew services cleanup is not clearing it.
Any way to uninstall a service? · Issue #118 · Homebrew/homebrew-services
From the docs (emphasis mine): $ brew help services: brew services Easily start and stop formulae via launchctl. With -v or --ve...

PostgreSQLのアンインストール

% brew uninstall postgresql@11

以上


【参考ページ】

macにbrewを使ってpostgreSQLをインストールする - Qiita
はじめに 備忘メモです。不足な手順等あると思いますが、温かい目でみてください。誤りがあればコメントいただけると幸いです。 PostgreSQLのインストール brewをアップデートする # brew update ...
PostgresSQLの初期設定 - Qiita
PostgresSQLでの基本操作を説明します。 ほとんど使用しないかもしれませんが、自身の環境で必要だったので備忘録がてら書いていきます。 ユーザ PostgresSQLは少し特殊(これが普通かもしれないが)で、ユーザ単体の概...
PostgreSQLのカレントスキーマ設定 - Qiita
Postgre のデフォルトでは、publicがカレントスキーマとなっている。 作成したスキーマのオブジェクトを参照するには、カレントスキーマを変更する必要があります。 -- カレントスキーマ確認。複数ある場合は最初のスキーマが表示...
psqlプロンプト上でユーザーを切り替える方法
psqlプロンプト上でユーザーを切り替える方法
IT
広告
一郎くんどっとこむ