(DBConnection.ConnectionError) tcp连接(localhost: 5432):连接被拒绝 - :econnrefused。

5

我正在Buddy持续集成服务器上运行构建,但在执行mix test时,在postgrex/ecto部分出现错误导致构建失败:

    ==> postgrex
Compiling 61 files (.ex)
Compiling lib/postgrex/protocol.ex (it's taking more than 10s)
Compiling lib/postgrex/default_types.ex (it's taking more than 10s)
Generated postgrex app
==> ecto
Compiling 69 files (.ex)
Compiling lib/ecto/query/builder/lock.ex (it's taking more than 10s)
Compiling lib/ecto/repo/queryable.ex (it's taking more than 10s)
Compiling lib/ecto/query/inspect.ex (it's taking more than 10s)
Compiling lib/mix/tasks/ecto.drop.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/filter.ex (it's taking more than 10s)
Compiling lib/ecto/repo/preloader.ex (it's taking more than 10s)
Compiling lib/mix/tasks/ecto.gen.repo.ex (it's taking more than 10s)
Compiling lib/ecto/adapters/mysql.ex (it's taking more than 10s)
Compiling lib/ecto/schema.ex (it's taking more than 10s)
Compiling lib/ecto/migration/runner.ex (it's taking more than 10s)
Compiling lib/ecto/repo/schema.ex (it's taking more than 10s)
Compiling lib/ecto/embedded.ex (it's taking more than 10s)
Compiling lib/ecto/migration/schema_migration.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/preload.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/order_by.ex (it's taking more than 10s)
Compiling lib/ecto/uuid.ex (it's taking more than 10s)
Compiling lib/ecto/type.ex (it's taking more than 10s)
Compiling lib/ecto/association.ex (it's taking more than 10s)
Compiling lib/ecto/adapters/postgres/connection.ex (it's taking more than 10s)
Compiling lib/mix/tasks/ecto.gen.migration.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/join.ex (it's taking more than 10s)
Compiling lib/ecto/query/planner.ex (it's taking more than 10s)
Compiling lib/ecto/log_entry.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/select.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/distinct.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/limit_offset.ex (it's taking more than 10s)
Compiling lib/ecto/changeset.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder/group_by.ex (it's taking more than 10s)
Compiling lib/ecto/changeset/relation.ex (it's taking more than 10s)
Compiling lib/ecto/multi.ex (it's taking more than 10s)
Compiling lib/ecto.ex (it's taking more than 10s)
Compiling lib/ecto/query/builder.ex (it's taking more than 10s)
Compiling lib/ecto/adapters/postgres.ex (it's taking more than 10s)
Generated ecto app
==> phoenix_ecto
Compiling 4 files (.ex)
Generated phoenix_ecto app
==> phoenix_chat
Compiling 19 files (.ex)
Generated phoenix_chat app
08:49:48.868 [error] GenServer #PID<0.3296.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
    (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
** (Mix) The database for PhoenixChat.Repo couldn't be created: an exception was raised:
    ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
        (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
        (connection) lib/connection.ex:622: Connection.enter_connect/5
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Build failed !!!.

我认为这是因为构建服务器似乎是 (localhost:5432),而且我不认为它与通常在端口4000上的Phoenix兼容。请参阅此答案以了解我的结论。 那么解决方案是什么?
我更改了dev.ex以具有此配置:
config :phoenix_chat, PhoenixChat.Endpoint,
  http: [port: {:system, "PORT"}],

然后我将任务设置为PORT=4000 mix test,但仍然遇到相同的错误。 编辑:似乎我的端口设置没有生效,因为错误仍然是:
PORT=4000 mix test
09:18:34.884 [error] GenServer #PID<0.183.0> terminating
** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
    (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
    (connection) lib/connection.ex:622: Connection.enter_connect/5
    (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
** (Mix) The database for PhoenixChat.Repo couldn't be created: an exception was raised:
    ** (DBConnection.ConnectionError) tcp connect (localhost:5432): connection refused - :econnrefused
        (db_connection) lib/db_connection/connection.ex:148: DBConnection.Connection.connect/2
        (connection) lib/connection.ex:622: Connection.enter_connect/5
        (stdlib) proc_lib.erl:247: :proc_lib.init_p_do_apply/3
Build failed !!!.

所以我现在认为解决方案可能是this。我不太了解如何添加ssh密钥。


这不是Phoenix的HTTP端口的问题。问题出在CI服务器上的localhost:5432没有运行PostgreSQL。你可能需要在CI的某个地方启用它,或者如果他们在不同的端口上运行PostgreSQL,则需要更改配置。 - Dogbert
@Dogbert 更改 dev.exs 中的配置为 config :phoenix_chat, PhoenixChat.Repo, adapter: Ecto.Adapters.Postgres, username: "postgres", password: "postgres", database: "phoenix_chat_dev", hostname: "localhost:5432", pool_size: 10 是否正确?(没有任何区别 - 相同的错误)- 更改是主机名。 - BeniaminoBaggins
应该是 hostname: "localhost", port: 5432, ...,但是5432是默认端口,错误提示说PostgreSQL在localhost:5432上没有运行。您应该找出CI服务器上PostgreSQL正在运行的主机/端口,如果它被默认禁用(例如在Travis中,您必须添加配置来启用PostgreSQL:https://docs.travis-ci.com/user/database-setup/#PostgreSQL)。 - Dogbert
4个回答

15
我的笔记本电脑崩溃了,在 /usr/local/var/postgres 中留下了一个 postmaster.pid 文件。

删除后,我的 Postgres 又恢复正常工作了。

如果你想检查 Postgres 是否正常运行,可以运行 pg_isready 来获取输出。你应该会得到: /tmp:5432 - accepting connections - 你的端口可能会因配置而异。

祝玩得开心!


如果我们收到 /tmp:5432 - no response 的错误,该如何解决? - Andrew Bregman
1
啊,我只需要启动我的PostgreSQL就可以了。 - Andrew Bregman

4

也可以尝试 brew services stop postgres 然后再执行 brew services start postgres。这解决了我的问题。


1

使用以下命令停止当前正在运行的Postgres

pg_ctl stop

使用以下命令重新启动 Postgres

pg_ctl start

1

你可能会遇到libssl/libcrypto版本冲突的问题。

brew services start postgres 命令没有任何日志信息,但是手动启动 pg_ctl -D /usr/local/var/postgres start 发现以下错误:

/.../openssl/lib/libcrypto.1.0.0.dylib 缺少 _RAND_cleanup 定义

尝试执行以下操作:

brew tap-new $USER/old-openssl
brew extract --version=1.0.2t openssl $USER/old-openssl
brew install openssl@1.0.2t

# and then either brew link openssl@1.0.2t or symlink the required files to current openssl
ln -s /usr/local/Cellar/openssl/openssl@1.0.2t/lib/libcrypto.1.0.0.dylib /usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib
ln -s /usr/local/Cellar/openssl/openssl@1.0.2t/lib/libssl.1.0.0.dylib /usr/local/opt/openssl/lib/libssl.1.0.0.dylib

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接