気軽に楽しくプログラムと遊ぶ

自分が興味があってためになるかもって思う情報を提供しています。

PostgreSQLの初期設定

PostgresSQLをインストール

$ brew install postgres

PostgreSQLサーバーの起動

自動起動の設定を以下で行います。

$ ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
/Users/user_name/Library/LaunchAgents/homebrew.mxcl.postgresql.plist -> /usr/local/opt/postgresql/homebrew.mxcl.postgresql.plist
~
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

停止したい場合は、以下コマンドを実行

$ launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

ログイン

% psql -U postgres
psql (9.5.0)
Type "help" for help.

※現時点、9.5.0は古いので最新を入れて下さい。。 10系を入れて下さい。

ロール(ユーザー)作成

今回は、postgres以外のユーザーが欲しかったので、以下で作成

postgres=# create role user_name with superuser createdb login password 'passString';
CREATE ROLE
postgres=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB              | {}
 user_name | Superuser, Create DB                           | {}

DB作成

一旦、¥qでログアウトした後、作成したユーザーで新しいDBを作成します。
¥lで作成DBを一覧で確認します。

postgres=# \q

$ psql -U user_name -d postgres
psql (9.5.0)
Type "help" for help.

postgres=# create database new_db;
CREATE DATABASE

postgres=# \l
                                 List of databases
     Name     |  Owner   | Encoding |   Collate   |    Ctype    | Access privileges
--------------+----------+----------+-------------+-------------+-------------------
 postgres     | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 new_db        | user_name  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

あとは、テーブル作成、データ投入など行って、初期設定を整えて下さい。