可以通过外壳连接到postgres容器,但不能通过phoenix应用程序连接

时间:2019-06-11 12:38:49

标签: postgresql docker docker-compose elixir phoenix-framework

我使用这种配置启动了一个postgres容器:

# Version of docker-compose
version: '3'

# Containers we are going to run
services:
  db:
    # We use the predefined Postgres image
    image: postgres:12
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres_db
    ports:
      - 5432:5432
    restart: on-failure

并且可以像预期的那样通过容器外部的本地外壳进行连接,因为端口5432已暴露:

psql -h localhost postgres_db postgres

但是奇怪的是phoenix无法连接,phoenix config如下:

# Configure your database
config :phoenix_chat, PhoenixChat.Repo
  username: "postgres",
  password: "postgres",
  database: "postgres_db",
  hostname: "localhost",
  pool_size: 10,
  # Help diagnose connection errors:
  show_sensitive_data_on_connection_error: true

它崩溃了:

Generated phoenix_chat app
[error] GenServer #PID<0.382.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[error] GenServer #PID<0.414.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[error] Postgrex.Protocol (#PID<0.380.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.384.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.383.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.388.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.385.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.389.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.381.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.387.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] Postgrex.Protocol (#PID<0.386.0>) failed to connect: ** (DBConnection.ConnectionError) awaited on another connection that failed to bootstrap types
[error] GenServer #PID<0.416.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[error] GenServer #PID<0.417.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[info] Running PhoenixChatWeb.Endpoint with cowboy 2.6.3 at 0.0.0.0:4000 (http)
[info] Access PhoenixChatWeb.Endpoint at http://localhost:4000
[error] GenServer #PID<0.524.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[error] GenServer #PID<0.530.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil
State: Postgrex.Protocol
[error] GenServer #PID<0.527.0> terminating
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")
    (elixir) lib/enum.ex:1327: Enum."-map/2-lists^map/1-0-"/2
    (postgrex) lib/postgrex/utils.ex:68: Postgrex.Utils.parse_version/1
    (postgrex) lib/postgrex/protocol.ex:796: Postgrex.Protocol.bootstrap_send/6
    (postgrex) lib/postgrex/protocol.ex:577: Postgrex.Protocol.handshake/2
    (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Last message: nil

我还尝试使用iex通过postgrex外壳进行连接

{:ok, pid} = Postgrex.start_link(hostname: "localhost", username: "postgres", password: "postgres", database: "postgres_db")

但是它也给出相同的错误。

postgres容器输出以下错误:

db_1  | 2019-06-11 12:38:05.127 UTC [542] LOG:  PID 528 in cancel request did not match any process
db_1  | 2019-06-11 12:38:05.205 UTC [557] LOG:  PID 549 in cancel request did not match any process
db_1  | 2019-06-11 12:38:05.244 UTC [575] LOG:  PID 570 in cancel request did not match any process

1 个答案:

答案 0 :(得分:-1)

iex(1)> :erlang.binary_to_integer("12beta1")
** (ArgumentError) argument error
    :erlang.binary_to_integer("12beta1")

iex(1)> :erlang.binary_to_integer("12")     
12

iex(2)> :erlang.binary_to_integer("12a")
** (ArgumentError) argument error
    :erlang.binary_to_integer("12a")

iex(2)> 

您是否命名了"12beta1"?如果有帮助,这是我的配置文件:

config :auction, ecto_repos: [Auction.Repo]

config :auction, Auction.Repo,
  database: "auction",
  username: "7stud",
  password: "",
  hostname: "localhost",
  port: "5432"

我的应用程序是一个伞形应用程序,其中phoenix应用程序auction_web不使用数据库,而auction应用程序及其用户界面使用数据库,并且数据库配置位于{ {1}}。 auction_umbrella/apps/auction/config/config.exs应用是auction_web目录中的另一个应用。