19
25

More than 5 years have passed since last update.

Mac+mamp+postgresqlの環境つくり

Posted at

PostgreSQLはmacにインストールすることができる。
mampにインストールするというより、macにインストールしてからmampで使えるようにするという感じになるようです。

参考:
MacにPostgreSQLをインストール
MacでPostgreSQLを使えるようにする
PostgreSQLをMAMP環境にインストールする

  • 全体の流れ
    -- PostgreSQLをインストールするためにHomebrewを最新版にして実行する
    -- PostgreSQLの環境を作る。(文字コード指定、ユーザ設定、DB作成とか。)

< Homebrewについて >
homebrewはmacのパッケージ管理システムのことです。詳細はこちらをみてください。パッケージ管理システム Homebrew

PostgreSQLはhomebrewでインストールします。

1.Homebrewのアップデート

現在のhomebrewのバージョンを確認し、最新の状態にします。

自分環境ではhomebrewのバージョンは変わらなくても細かい何かが変わったようです。
$ brew -v
Homebrew 0.9.5 (git revision a893; last commit 2016-01-05)
$ brew update 
Updated Homebrew from a89340bf to 613a2d9d.
==> New Formulae
:
groonga-normalizer-mysql  
:
$ brew -v
Homebrew 0.9.5 (git revision 613a; last commit 2016-01-21)

2.PostgreSQLのインストール

$ brew install postgres
 :
==> Summary
🍺  /usr/local/Cellar/postgresql/9.5.0: 3,113 files, 35.0M
$ postgres -V
postgres (PostgreSQL) 9.5.0

インストールされました!

<脱線>
好きなバージョンのインストールもできるようです。
homebrewのversionsライブラリを使うことで過去バージョンを探し出せれるようです。

postgresql8をさがしてみる
$ brew tap homebrew/versions  <ーこれをしないとバージョン探せないみたい
==> Tapping homebrew/versions
Cloning into '/usr/local/Library/Taps/homebrew/homebrew-versions'...
remote: Counting objects: 268, done.
remote: Compressing objects: 100% (249/249), done.
remote: Total 268 (delta 38), reused 75 (delta 19), pack-reused 0
Receiving objects: 100% (268/268), 288.67 KiB | 26.00 KiB/s, done.
Resolving deltas: 100% (38/38), done.
Checking connectivity... done.
Tapped 264 formulae (289 files, 1004.5K)
$ brew info postgresql8 
homebrew/versions/postgresql8: stable 8.4.22
Object-relational database system
 :
 :
$ brew untap homebrew/versions <ーtapが必要なければ消せばいい。
Untapping homebrew/versions... (289 files, 1004.5K)
Untapped 264 formulae
$ brew info postgresql8
Error: No available formula with the name "postgresql8"  <ー探せなくなる。

tapコマンドがよくわからないので、こちらを参考にしてみてください。

2.1.文字コードを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".

2.2. PosgreSQLサーバーを起動

$ postgres -D /usr/local/var/postgres
LOG:  database system was shut down at 2016-01-21 19:45:27 JST
LOG:  MultiXact member wraparound protections are now enabled
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections

 <この状態で止まる>

起動したままで、別のitermを開いてデータベース一覧が取得出来ているか確認する

$ psql -l
                               List of databases
   Name    | Owner  | Encoding |   Collate   |    Ctype    | Access privileges 
-----------+--------+----------+-------------+-------------+-------------------
 postgres  | tago | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | 
 template0 | tago | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/tago        +
           |        |          |             |             | tago=CTc/tago
 template1 | tago | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/tago        +
           |        |          |             |             | tago=CTc/tago
(3 rows)

サーバの起動を止めて(Ctr+C)同じように一覧を取得しようとするとエラーになります。

$ psql -l
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

<postgresの起動と停止>
起動
$ pg_ctl start -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log
終了
$ pg_ctl stop -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log

2.3.PostgreSQLのユーザをつくる

ユーザは環境変数$USER、つまり、macで入っている自分のIDでつくられているらしいのでpostgres用のユーザを作ります。
postgresは起動していないとダメです。

postgresっていうユーザをつくった
$ createuser -P postgres
Enter password for new role: 
Enter it again: 
$ psql -q -c'select * from pg_user' postgres
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig 
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
  $USER  |       10 | t           | t        | t       | t            | ******** |          | 
 postgres |    16384 | f           | f        | f       | f            | ******** |          | 
(2 rows)

2.4.DBの作成

$ createdb tagotest -O postgres  <ーオプションoでオーナー指定
$ psql -U postgres tagotest  <ーDBにアクセス
psql (9.5.0)
Type "help" for help.

tagotest=>

おまけのphpPgAdmin

これで、mampからもつながっちゃうはずなんですが、
GUIじゃないとわかんない。。って場合はphpPgAdminをいれてみてください。
20160122時点でphpPgAdmin-5.1が最新のようです。
解凍した「phpPgAdmin-5.1」ファイルをMAMPの環境下にリネームしておきます。

$ pwd
/Applications/MAMP/bin
$ cp -r ~/Desktop/phpPgAdmin .

最近のmamp(バージョン3.3)では httpd.confの設定も用意されているみたいです。
/Applications/MAMP/conf/apache/httpd.conf 378行目あたりに記載されてる。
http://localhost:8888/phppgadmin
で表示されることを確認。
最初のログインは、マシンアカウントのID/PWで入るみたいですね。。

19
25
0

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
19
25