LoginSignup
490
473

More than 5 years have passed since last update.

MacにPostgreSQLをインストール

Last updated at Posted at 2014-12-23

環境:Mac OS X 10.9.4

Homebrewを使用してインストール

$ brew install postgresql
:
:
==> /usr/local/Cellar/postgresql/9.3.4/bin/initdb /usr/local/var/postgres
==> Summary
?  /usr/local/Cellar/postgresql/9.3.4: 2921 files, 38M

文字コードをUTF-8でデータベースの初期化

$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user "dai".
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".

Postgresのバージョンチェック

$ postgres --version
postgres (PostgreSQL) 9.3.4

PostgreSQLサーバの起動

$ postgres -D /usr/local/var/postgres
LOG:  database system was shut down at 2014-07-09 00:37:58 JST
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

psql -lでデータベース一覧を取得

データベースの一覧が取得できたら完了。

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

DBの置き場所を環境変数の設定

.bash_profile
# PostgreSQL設定(DBの置き場所)
export PGDATA=/usr/local/var/postgres

pg_ctlでPostgreSQLの起動と停止

pg_sqlとは

pg_ctlはPostgreSQL バックエンドサーバであるpostmaster を起動、停止、再起動、 あるいは起動中の postmaster の状態を表示するためのユーティリティだそうです。

通常次の様に指定して起動と停止を行う。
[起動]

$ pg_ctl -D /usr/local/pgsql/data -l /var/log/postgres start

[停止]

$ pg_ctl -D /usr/local/pgsql/data stop

環境変数PGDATAを設定していると、pg_ctlでの起動停止時に-Dの引数を渡さなくてよくなります。

[起動]

$ pg_ctl -l /usr/local/var/postgres/server.log
server starting
$

[停止]

$ pg_ctl stop
waiiting for server to shut down.... done
server stoped
$

ユーザーの作成と確認

ユーザーの作成

環境変数$USER がスーパーユーザとして作成されており、そのまま使うのはあまり良くないので、新しくユーザを作成する。
次のコマンドでPostgresユーザーを作成できるが、事前にpostgreSQLのサービスが開始されている必要があります。
また、ユーザ作成時にパスワードを付与するためには-Pオプションを使用します。

$ createuser -P <pg-user> 
Enter password for new role: 
Enter it again: 

作成されたユーザーの確認

$ psql -q -c'select * from pg_user' <pg-user>
 usename  | usesysid | usecreatedb | usesuper | usecatupd | userepl |  passwd  | valuntil | useconfig 
----------+----------+-------------+----------+-----------+---------+----------+----------+-----------
 myuser   |       10 | t           | t        | t         | t       | ******** |          | 
 postgres |    16384 | f           | f        | f         | f       | ******** |          | 
(2 rows)

DBの作成

DBはcreatedbコマンドで作成します。今回、DB名にはexample-dbとしています。

$ createdb example-db -O <pg-user>

オプション

  • -O [オーナー] or (--owner)
    作成するデータベースの所有者となるユーザを指定します。

pslでDBに接続

DB接続

psqlコマンドでDBに接続してSQLなどの発行が行えます。

$ psql -U <pg-user> example-db
psql(9.4.0)
Type "help" for help

example-db=>

オプション

  • -f [ファイル名] or (--filename)
    指定したファイルを問い合わせのソースとして使用します。
  • -U [接続ユーザ名] or (--username)
    PostgreSQLに接続するユーザ名を指定します。
  • -h [ホスト名] or (--hostname)
    PostgreSQLに接続するホスト名を指定します。

DBとテーブル情報を確認

コマンド 説明
\l DB一覧
\c [DB名] DBの切り替え
\d    テーブル一覧
\d [テーブル名] テーブルの項目(フィールド)確認
\q psqlを終了する
490
473
2

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
490
473