LoginSignup
24
30

More than 3 years have passed since last update.

MacにPostgreSQL9.6.3をインストールしてみた

Last updated at Posted at 2017-05-12

環境

  • OS:macOS Sierra 10.12.3
  • Homebrew:1.2.0 Homebrew/homebrew-core (git revision 5045; last commit 2017-05-11)
  • PostgreSQL:9.6.3

インストール

PostgreSQLのインストール

Homebrewを使えば一発でインストールできます。
Homebrewを最新にしてからPostgreSQLをインストールします。

$ brew update
$ brew install postgresql
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.6.3.sierra.bot
######################################################################## 100.0%
==> Pouring postgresql-9.6.3.sierra.bottle.tar.gz
==> Using the sandbox
==> /usr/local/Cellar/postgresql/9.6.3/bin/initdb /usr/local/var/postgres
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
  https://github.com/Homebrew/legacy-homebrew/issues/2510

To migrate existing data from a previous major version (pre-9.0) of PostgreSQL, see:
  https://www.postgresql.org/docs/9.6/static/upgrading.html

To migrate existing data from a previous minor version (9.0-9.5) of PostgreSQL, see:
  https://www.postgresql.org/docs/9.6/static/pgupgrade.html

  You will need your previous PostgreSQL installation from brew to perform `pg_upgrade`.
  Do not run `brew cleanup postgresql` until you have performed the migration.

To have launchd start postgresql now and restart at login:
  brew services start postgresql
Or, if you don't want/need a background service you can just run:
  pg_ctl -D /usr/local/var/postgres start
==> Summary
🍺 /usr/local/Cellar/postgresql/9.6.3: 3,259 files, 36.6MB

エラーが出力されているように見えるのですが、Google翻訳で確認したところ、ただの警告でした。
すでにPostgreSQLがインストールされている場合の注意点と、PostgreSQLの起動方法が出力されています。

バージョンの確認

インストールできたかどうか、バージョンを確認してみます。

$ psql --version
psql (PostgreSQL) 9.6.3

9.6.3 と表示されているため、インストールに成功しています。

初期化

-E オプションで文字コードにUTF-8を指定してDBを初期化します。
初期化時にデータベースクラスタというDBの集合体を作成します。

$ 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 で起動します。

$ brew services start postgresql
==> Tapping homebrew/services
Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (7/7), done.
remote: Total 10 (delta 0), reused 5 (delta 0), pack-reused 0
Unpacking objects: 100% (10/10), done.
Tapped 0 formulae (37 files, 51KB)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

私の環境ではキーチェーンの確認が2回ほどありましたので、両方とも「許可」を選択しました。

DB一覧の取得

psql -l でDBの一覧を取得します。

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

初期化直後のデータベースクラスタには3つのDBが存在します。

DBの作成

createdb {DB名} でDBを新規作成します。ここでは「test」という名前のDBを作成します。

$ createdb test

DBへの接続

psql {DB名} でDBへ接続します。接続することでSQLの発行が行えます。

$ psql test
psql (9.6.3)
Type "help" for help.

test=#

ここでは省略しませんでしたが、ユーザー名とDB名が同一の場合にはDB名を省略できます。

テーブルの作成

CREATE TABLE でテーブルを作成します。ここでは「test_table」という名前のテーブルを作成します。

test=# create table test_table (
test(# id integer not null,
test(# name varchar(20),
test(# primary key(id)
test(# )
test(# ;
CREATE TABLE

基本コマンド

PostgreSQLでは \ から始まる便利なコマンドがいろいろ用意されています。

コマンド 説明
\l DB一覧取得
\d テーブル一覧取得
\d {テーブル名} 対象テーブルのフィールド一覧取得
\i {SQLファイルパス} SQLファイル実行
\q DB切断

DB一覧の取得

psql -l と同様なので説明を省略します。

テーブル一覧の取得

\d でテーブル一覧を取得します。

test=# \d
             List of relations
 Schema |    Name    | Type  |    Owner     
--------+------------+-------+--------------
 public | test_table | table |  {user name}
(1 row)

フィールド一覧の取得

\d {テーブル名} で対象テーブルのフィールドの一覧を取得します。

test=# \d test_table
         Table "public.test_table"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 name   | character varying(20) | 
Indexes:
    "test_table_pkey" PRIMARY KEY, btree (id)

SQLファイルの実行

\i {SQLファイルパス} で予め用意したファイルに記述されているSQLをまとめて実行します。

test.sql
insert into test_table (id, name) values (1, 'hoge');
insert into test_table (id, name) values (2, 'huga');
test=# \i ~/Desktop/test.sql
INSERT 0 1
INSERT 0 1
test=# select * from test_table;
 id | name
----+------
  1 | hoge
  2 | huga
(2 rows)

DBの切断

\q でDBを切断します。

test=# \q

サービスの停止

brew services stop postgresql でDBを停止します。
brew services list でサービスの起動状況を取得します。

Status 説明
started 起動中
stopped 停止中
$ brew services list
Name       Status  User         Plist
postgresql started {user name}  /Users/{user name}/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

$ brew services stop postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)

$ brew services list
Name       Status  User Plist
postgresql stopped

参考リンク

24
30
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
24
30