LoginSignup
0
2

More than 5 years have passed since last update.

macOS への Postgres のインストール

Last updated at Posted at 2017-09-22

インストール

$ brew install postgresql

起動

$ brew services start postgresql
Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

(おまけ) サービスとして使えるもののリスト

$ brew services list            
Name       Status  User            Plist
memcached  started oh /Users/oh/Library/LaunchAgents/homebrew.mxcl.memcached.plist
mysql      started oh /Users/oh/Library/LaunchAgents/homebrew.mxcl.mysql.plist
nginx      stopped                 
postgresql started oh /Users/oh/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
redis      started oh /Users/oh/Library/LaunchAgents/homebrew.mxcl.redis.plist

Postgres のデータベース一覧

$ psql -U $USER -l
                                         List of databases
   Name    |      Owner      | Encoding | Collate | Ctype |            Access privileges            
-----------+-----------------+----------+---------+-------+-----------------------------------------
 postgres  | oh              | UTF8     | C       | UTF-8 | 
 template0 | oh              | UTF8     | C       | UTF-8 | =c/"oh"                   +
           |                 |          |         |       | "oh"=CTc/"oh"
 template1 | oh              | UTF8     | C       | UTF-8 | =c/"oh"                   +
           |                 |          |         |       | "oh"=CTc/"oh"
(3 rows)

postgres というデータベースが Owner=oh で作成されている。

データベースへの接続

デフォルトの postgres データベースに、owner (= "oh") として接続する:

psql -U $USER postgres

データベースの作成

mydb という名前のデータベースの作成:

$ createdb mydb

ユーザーの作成

(brew でインストールすると、$USER がスーパーユーザーとして自動的に登録される: 他の手段でインストールする場合は postgres をスーパーユーザーにすることが多い)

myuser の作成:

$  createuser myuser 

ユーザーが作成されたことの確認

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

postgres の REPL からのユーザーおよびデータベースの作成

$ psql -U $USER postgres
psql (9.6.5)
Type "help" for help.

postgres=# CREATE ROLE mrs LOGIN
postgres-#   UNENCRYPTED PASSWORD 'mrs'
postgres-#   NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;
CREATE ROLE

postgres=# CREATE DATABASE mrs
postgres-#   WITH OWNER = mrs
postgres-#     ENCODING = 'UTF8'
postgres-#     TABLESPACE = pg_default
postgres-#     LC_COLLATE = 'C'
postgres-#     LC_CTYPE = 'C'
postgres-#     TEMPLATE = 'template0'
postgres-#     CONNECTION LIMIT = -1;
CREATE DATABASE

なお、ここで作成している mrs ユーザーと mrs データベース (ついでに mrs というパスワード) は、「Spring徹底入門」の第14章で必要とされているデータベースの設定。

0
2
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
0
2