LoginSignup
13

More than 5 years have passed since last update.

MAPP 環境を構築する!!(PostgreSQL を Mac にインストール)

Last updated at Posted at 2016-06-29

普段は MAMP を利用して baserCMS や WordPress のローカル開発環境を行っているのですが、今回は PostgreSQL を利用しなければなりませんでした。

Mac, Apache, PostgreSQL, PHP の MAPP 環境を構築してみます!!

環境 : Mac OS X El Capitan 10.11.2

1. Homebrewで PostgreSQL をインストール

まずは下記コマンドを実行します。


$ brew install postgresql

既にインストールされているらしい...

Error: postgresql-9.5.2 already installed
To install this version, first `brew unlink postgresql`

会社の Mac なので前に利用してた方がインストールしていたようです。

インストールはされているけど、パスは通っていない状態らしいので、下記コマンドを実行し、 unlink してみます。

$ brew unlink postgresql

Unlinking /usr/local/Cellar/postgresql/9.5.2... 372 symlinks removed

またインストールし直します。

$ brew install postgresql

initdb が必要みたいなので下記コマンドを実行してみます。

$ 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".

※ 本来ならこれでログインして完了なのですが...

前に入っていた postgreSQL のデータベースのバージョンと、今回インストールした postgreSQL のバージョンが違っており、ログインする時にエラーが出ていました。

ですので、前に入っていたものを削除してから再インストールし直すことにします...笑

ディレクトリを削除します。

$ sudo rm -rf /usr/local/var/postgres

先ほど流したインストールのコマンドをもう一度実行していきます。

$ brew install postgresql
$ brew link postgresql
$ initdb /usr/local/var/postgres -E utf8

ここで新しくインストールし直したら、ログインします。

$ pg_ctl -D /usr/local/var/postgres -l logfile start
$ psql -d postgres

これで権限ユーザーにてログインできました!!!

参考サイト :

2. 接続用のユーザをつくる

今回は hoge ユーザを作成します。

# create user hoge;
CREATE ROLE

CREATE ROLE と表示されれば作成完了!

ユーザ hoge にパスワード hogepass を設定します。

# \password hoge

ユーザ一覧を確認して、新しく作成したユーザが表示されていればオーケー!

# \du

                                    List of roles
  Role name  |                         Attributes                         | Member of
-------------+------------------------------------------------------------+-----------
 hoge        |                                                            | {}
 **********  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

新しく作成した hoge ユーザーにスーパーユーザ権限を与えておきます。

hoge ユーザは何も権限を持たないと、データベースの作成、更新などが何も行えないので、権限を付与させる必要があります。

# ALTER ROLE hoge WITH SUPERUSER;

参考サイト :

3. 接続用のユーザでログインする

管理者ユーザから接続用ユーザ hoge に切り替えます。

# \connect - hoge

4. ログインしたら新しくdb作る

今回は hogeDB というデータベースを作成します。

create database hogeDB;

作成した DB を確認する

# \l

ちゃんと作成されていればオッケーです!

ログアウトは下記コマンドを使用します。

# \q

5. Adminer を使用してブラウザから PostgreSQL に接続する

  • https://www.adminer.org/en/#download よりphpファイルをダウンロードします。
    今回は https://www.adminer.org/static/download/4.2.5/adminer-4.2.5.php をダウンロードしました。
  • 任意のローカル開発環境にダウンロードしたファイルを「adminer.php」とリネームして設置します。
    私は baserCMS のプロジェクト(baser_project)の直下(app や lib 等と同じ階層)に設置しました。
  • ブラウザで、http://〇〇〇/adminer.php を表示すると、 adminer のログイン画面が表示されると思います。
    私の場合は、http://baser_project.localhost:8888/adminer.php
  • ログインの際は下記項目を入力してください。
    • データベース種類→ PostgreSQL
    • ユーザ名→ hoge
    • パスワード→ hogepass

参考サイト :

6. Adminer に既存の sql データをインポートする

ここからは、Adminerに既存の SQL データをインポートしていきます。

  • Adminer の対象データベース(hogeDB)を選択し、「インポート」より、SQL データを読み込ませます。
  • ここで問題発生...
    SQL データが重すぎたのか、Adminer のインポートの調子が悪いのか、なかなかインポートが完了しなかったので Adminer のウィンドウを一度閉じ、 MAMP で使用していた Apache を再起動します...笑

ターミナルよりSQLデータをインポート

  • インポートしたいSQL データ(今回は hogehoge.sql)のあるディレクトリまで移動します。
  • 下記コマンドを実行します。
$psql hogeDB hoge < hogehoge.sql

一瞬でインポートが完了しました!!

これで完了です!!!

PostgreSQL のコマンドに慣れていないので、下記サイトを参考にしながら慣れていこうと思います!!
http://qiita.com/mm36/items/1801573a478cb2865242

お疲れ様でした!!!

【追記】 BcPosgreに接続していない場合

下記エラーが表示される。

"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"?"

Posgre起動コマンドを実行する

$ pg_ctl -D /usr/local/var/postgres/ -l logfile start
$ psql -d postgres

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
13