Moro Blog

ゼロからのスタート

Macにpostgresqlを入れる

MacPostgreSQLを入れる

MacOS Mojave 10.14.2
postgres (PostgreSQL) 11.1


Homebrewでinstall

$ brew install postgresql
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/postgresql-11.1.mojave.bottle.tar.gz
######################################################################## 100.0%
==> Pouring postgresql-11.1.mojave.bottle.tar.gz
==> /usr/local/Cellar/postgresql/11.1/bin/initdb /usr/local/var/postgres
==> Caveats
To migrate existing data from a previous major version of PostgreSQL run:
  brew postgresql-upgrade-database

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/11.1: 3,548 files, 40.3MB

初期設定

UTF-8で起動する
その他初期設定→PostgreSQLの初期設定 | Netsphere Laboratories

$ 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 locales
  COLLATE:  C
  CTYPE:    UTF-8
  MESSAGES: C
  MONETARY: C
  NUMERIC:  C
  TIME:     C
initdb: could not find suitable text search configuration for locale "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サーバの起動

$ postgres -D /usr/local/var/postgres&
[1] 51495
2019-01-09 10:28:47.882 JST [51495] LOG:  listening on IPv6 address "::1", port 5432
2019-01-09 10:28:47.883 JST [51495] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-01-09 10:28:47.883 JST [51495] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-01-09 10:28:47.902 JST [51512] LOG:  database system was shut down at 2019-01-09 10:28:40 JST
2019-01-09 10:28:47.908 JST [51495] LOG:  database system is ready to accept connections


Database一覧の確認

$ psql -l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜〜

Database作成

$ createdb development
$ psql -l
                                   List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-------------+----------+----------+-------------+-------------+-----------------------
 development | morokado | UTF8     | en_US.UTF-8 | en_US.UTF-8 |

Databaseにログイン

$ psql development
psql (11.1)
Type "help" for help.

development=#