5
4

More than 5 years have passed since last update.

PhoenixをCloud9上でDBと接続する

Posted at

前回はWebサーバの起動までやりましたので、DBの設定をしてみましょう。
今回はデフォルトのPostgreを使います。なおMysqlはプロジェクト作成時に指定すれば使えるそうです。

とりあえず実行

とりあえずPhoenixというかElixirのDB操作ライブラリEctoのコマンドを叩いてみます。これはデータベースを作成するコマンドです。

~/workspace/hello_phoenix $ mix ecto.create
** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: psql: could not connect to server: Connection refused
        Is the server running on host "localhost" (::1) and accepting
        TCP/IP connections on port 5432?
could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

はい。とうぜん失敗します。DBサーバを立ち上げていません。

DB起動

DBを起動しましょう。
https://docs.c9.io/docs/setting-up-postgresql

~/workspace/hello_phoenix $ sudo service postgresql start
 * Starting PostgreSQL 9.3 database server
   ...done.

接続テスト

~/workspace/hello_phoenix $ sudo sudo -u postgres psql
psql (9.3.10)
Type "help" for help.

postgres=# \q
:~/workspace/hello_phoenix $ 

CLI上では繋がりました。

もっかい実行

~/workspace/hello_phoenix $ mix ecto.create
** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: psql: FATAL:  password authentication failed for user "postgres"
.

はい。まだ駄目です。認証に失敗してますね。

試しにパスワードを消してみる

CLIではパスワードなしで接続できたのでパスワードを消してみます。

config/dev.exs
config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "",
  database: "hello_phoenix_dev",
  hostname: "localhost",
  pool_size: 10

実行。

~/workspace/hello_phoenix $ mix ecto.create
** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: psql: FATAL:  password authentication failed for user "postgres"
.
qooq:~/workspace/hello_phoenix $ mix ecto.create
Compiled lib/hello_phoenix.ex
Compiled web/web.ex
Compiled web/views/error_helpers.ex
Compiled web/channels/user_socket.ex
Compiled web/gettext.ex
Compiled lib/hello_phoenix/repo.ex
Compiled web/router.ex
Compiled web/views/error_view.ex
Compiled web/views/page_view.ex
Compiled web/views/layout_view.ex
Compiled web/controllers/page_controller.ex
Compiled lib/hello_phoenix/endpoint.ex
Generated hello_phoenix app
** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: psql: fe_sendauth: no password supplied
.

ダメですね。パスワードは必要なようです。
パスワードを設定しましょう。

パスワードの設定

~/workspace/hello_phoenix $ sudo sudo -u postgres psql
psql (9.3.10)
Type "help" for help.

postgres=# \password
Enter new password: 
Enter it again: 
postgres=# \q
~/workspace/hello_phoenix $ 
config/dev.exs
config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "hello_phoenix_dev",
  hostname: "localhost",
  pool_size: 10

では実行

~/workspace/hello_phoenix $ mix ecto.create
Compiled lib/hello_phoenix.ex
Compiled web/views/error_helpers.ex
Compiled web/channels/user_socket.ex
Compiled web/web.ex
Compiled web/gettext.ex
Compiled lib/hello_phoenix/repo.ex
Compiled web/views/error_view.ex
Compiled web/router.ex
Compiled web/views/page_view.ex
Compiled web/controllers/page_controller.ex
Compiled web/views/layout_view.ex
Compiled lib/hello_phoenix/endpoint.ex
Generated hello_phoenix app
** (Mix) The database for HelloPhoenix.Repo couldn't be created, reason given: ERROR:  22023: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT:  Use the same encoding as in the template database, or use template0 as template.
LOCATION:  createdb, dbcommands.c:359

今度はまた別のエラーで失敗しました。どうもテンプレートというものが指定されてないようです。PostgreではDBの作成はテンプレートをコピーするという形で行われているようです。
https://chodounsky.net/2015/05/26/elixir-ecto-database-create-failed-on-sql-ascii/

テンプレートの指定

ではテンプレートを指定しましょう。

config/dev.exs
config :hello_phoenix, HelloPhoenix.Repo,
  adapter: Ecto.Adapters.Postgres,
  username: "postgres",
  password: "postgres",
  database: "hello_phoenix_dev",
  hostname: "localhost",
  template: "template0",
  pool_size: 10

ではもう一度です。

~/workspace/hello_phoenix $ mix ecto.create
Compiled lib/hello_phoenix.ex
Compiled web/channels/user_socket.ex
Compiled web/views/error_helpers.ex
Compiled web/web.ex
Compiled web/gettext.ex
Compiled lib/hello_phoenix/repo.ex
Compiled web/router.ex
Compiled web/views/error_view.ex
Compiled web/controllers/page_controller.ex
Compiled web/views/page_view.ex
Compiled web/views/layout_view.ex
Compiled lib/hello_phoenix/endpoint.ex
Generated hello_phoenix app
The database for HelloPhoenix.Repo has been created.

作成できました!いちおうCLIで確認しましょう。

~/workspace/hello_phoenix $ sudo sudo -u postgres psql
psql (9.3.10)
Type "help" for help.

postgres=# \list
                                 List of databases
       Name        |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-------------------+----------+-----------+---------+-------+-----------------------
 hello_phoenix_dev | postgres | UTF8      | C       | C     | 
 postgres          | postgres | SQL_ASCII | C       | C     | 
 template0         | postgres | SQL_ASCII | C       | C     | =c/postgres          +
                   |          |           |         |       | postgres=CTc/postgres
 template1         | postgres | SQL_ASCII | C       | C     | =c/postgres          +
                   |          |           |         |       | postgres=CTc/postgres

hello_phoenix_devができていますね。
これでDBとの接続に成功です。

5
4
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
5
4