LoginSignup
83
72

More than 5 years have passed since last update.

macにbrewを使ってpostgreSQLをインストールする

Last updated at Posted at 2019-02-27

はじめに

備忘メモです。不足な手順等あると思いますが、温かい目でみてください。誤りがあればコメントいただけると幸いです。

PostgreSQLのインストール

brewをアップデートする

# brew update

裏で重いファイルのダウンロード処理をしていたためか時間がかかりました。

PostgreaSQLをインストールする

# brew install postgresql

//最後にこんなのが出ますが、今回は無視します。
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

何もしていないと最新のバージョンがダウンロードされます。バージョンを指定したい場合は以下のコマンドでバージョンを検索してから、バージョンを指定してダウンロードすると良さそうです。

# brew search postgresql
# brew install <検索結果のバージョン>

PostgreSQLの初期設定

.bash_profileを更新する

以下のコマンドでバージョンを確認します。(パスでバージョンわかってるのですが・・・)

# ./usr/local/Cellar/postgresql/11.2/bin/psql --version
psql (PostgreSQL) 11.2

psqlコマンドのパスを通すために以下のコマンドで.bash_profileを編集します。余談ですが.bash_profileはターミナル起動時、.basharcはシェル起動時に呼び出されます。

# vi ~/.bash_profile
以下を追記
export PATH=/usr/local/Cellar/postgresql/10.1/bin/:$PATH

編集後、以下のコマンドで1.bash_profile1を読み込みます。もしくは、ターミナルを開き直して読み込みます。

# source ~/.bash_profile

データベースを作成

以下のコマンドでpostgreSQLを起動します。

# brew services start postgresql

postgresqlにログインします。

# psql postgres

上記はmacのユーザ名でログインしています。何かと不便なのでpostgresのロールを作成します。

# postgres=# create user postgres SUPERUSER;

postgresユーザでログインしなおします。

# psql postgres

データベースを確認します。

xxxxxxxxxxはmacのユーザ名
postgres=# \l
                                     List of databases
   Name    |   Owner    | Encoding |   Collate   |    Ctype    |     Access privileges     
-----------+------------+----------+-------------+-------------+---------------------------
 postgres  | xxxxxxxxxx | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | xxxxxxxxxx | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/xxxxxxxxxx            +
           |            |          |             |             | xxxx=CTc/xxxxxxxxxx
 template1 | xxxxxxxxxx | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/xxxxxxxxxx            +
           |            |          |             |             | xxxxxxxxxx=CTc/xxxxxxxxxx

適当なデータベースを作成します。

postgres=# create database hogehoge owner=postgres;
CREATE DATABASE

hogehogeデータベースが作成されていることを確認します。

xxxxxxxxxxはmacのユーザ名
postgres=# \l
                                     List of databases
   Name    |   Owner    | Encoding |   Collate   |    Ctype    |     Access privileges     
-----------+------------+----------+-------------+-------------+---------------------------
 hogehoge  | postgres   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | xxxxxxxxxx | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | xxxxxxxxxx | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/xxxxxxxxxx            +
           |            |          |             |             | xxxxxxxxxx=CTc/xxxxxxxxxx
 template1 | xxxxxxxxxx | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/xxxxxxxxxx            +
           |            |          |             |             | xxxxxxxxxx=CTc/xxxxxxxxxx
(4 rows)

hogehogeデータベースにログインします。

# psql hogehoge -U postgres

適当なテーブルを作成します。

hogehoge=# CREATE TABLE account(id varchar(8), name varchar(8));
CREATE TABLE

テーブルが作成されているか確認します。

hogehoge=# \d
          List of relations
 Schema |  Name   | Type  |  Owner   
--------+---------+-------+----------
 public | account | table | postgres

PostgreSQLを停止する

一通り操作できたので、以下のコマンドで停止します。

# brew services stop postgresql

参考記事

以下の記事を参考にさせていただきました。
https://qiita.com/gooddoog/items/1f986c1a6c0f253bd4e2
http://ponsuke-tarou.hatenablog.com/entry/2018/01/31/232012

83
72
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
83
72