weblog

技術的なメモ置き場。

macOSにPostgreSQLをインストール

Homebrewを使用して、macOSPostgreSQLをインストールしたので、その時の記録として残す。

環境

macOS (10.12.1) PostgreSQL (9.6.1) Homebrew (1.1.2)

Homebrewのアップデート

$ brew update
$ brew doctor

PostgreSQLのインストール

$ brew install postgresql

初期化

$ initdb /usr/local/var/postgres -E utf8

実行すると以下のようなメッセージが表示された。 指示に従って /usr/local/var/postgres を削除してから再実行。

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".

/usr/local/var/postgres でエラーが発生するのは、HomebrewでPostgreSQLをインストールする際に内部でinitdbが実行されるかららしい)

今度は以下のようなワーニングが発生したが、今回はtrust認証で問題ないのでスルー。

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

※ trust認証については公式サイト参照。 https://www.postgresql.jp/document/9.6/html/auth-methods.html#auth-trust

起動

$ pg_ctl -D /usr/local/var/postgres -l logfile start

データベース一覧を取得してみる

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

停止

$ pg_ctl -D /usr/local/var/postgres stop -s -m fast

本当に停止したか確認してみる

先ほどのデータベース一覧取得を再度行ってみると停止していることが分かる。

$ psql -l
psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

権限設定

postgresユーザにSuperuser権限を設定する。

$ psql -U admin postgres
# ALTER ROLE postgres WITH Superuser;
# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 admin     | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 postgres  | Superuser                                                  | {}

参考

http://tstomoki.com/programming/posgre_inst http://qiita.com/_daisuke/items/13996621cf51f835494b

Spring BootでセッションIDを取得する

意外とセッションIDの取得方法が見当たらなかったのでメモ。
Controllerメソッドの引数に、HttpSessionかHttpServletRequestを使用することでセッションIDを取得することができる。

public class HogeController {

    // HttpSessionを使用する場合
    @GetMapping
    String index(HttpSession session) {
        String sessionId = session.getId();
        return "index";
    }

    // HttpServletRequestを使用する場合
    @GetMapping
    String index(HttpServletRequest request) {
        String sessionId;
        sessionId = request.getSession().getId();
        // もしくは↓
        sessionId = WebUtils.getSessionId(request);
        return "index";
    }
}

 

 

Spring徹底入門 Spring FrameworkによるJavaアプリケーション開発