我有一个 Elixir/Phoenix 应用程序,我一直在本地开发,由 PostgreSQL 支持。当我使用
mix phx.server
通过终端运行时,一切正常。
我现在正在尝试对应用程序进行 Docker 化,以便可以部署到 AWS。我正在使用 Distillery 遵循使用 Docker 进行部署文档。
我已经从该页面逐字复制了所有设置,因此请随意查看这些配置,因为我使用的是相同的配置。
当我尝试运行
docker-compose up
时,出现以下错误:
...
db_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
web_1 | 12:05:31.631 [error] Postgrex.Protocol (#PID<0.1879.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:32.093 UTC [54] FATAL: password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:32.093 UTC [54] DETAIL: Password does not match for user "postgres".
db_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
web_1 | 12:05:32.094 [error] Postgrex.Protocol (#PID<0.1877.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:32.295 UTC [55] FATAL: password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:32.295 UTC [55] DETAIL: Password does not match for user "postgres".
db_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
web_1 | 12:05:32.296 [error] Postgrex.Protocol (#PID<0.1870.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:32.640 UTC [56] FATAL: password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:32.640 UTC [56] DETAIL: Password does not match for user "postgres".
db_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
web_1 | 12:05:32.641 [error] Postgrex.Protocol (#PID<0.1880.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:33.150 UTC [57] FATAL: password authentication failed for user "postgres"
db_1 | 2020-04-14 12:05:33.150 UTC [57] DETAIL: Password does not match for user "postgres".
db_1 | Connection matched pg_hba.conf line 95: "host all all all md5"
web_1 | 12:05:33.151 [error] Postgrex.Protocol (#PID<0.1875.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
...
这可能是一个相当简单的问题,但我没有做过太多数据库管理工作。我的猜测是我需要更改
config/docker.env
文件中的某些内容(来自上面的链接)。
如果我没有记错的话,
postgres
用户的默认密码为空。但我不确定如何使用该 docker.env
文件设置空白密码。理想情况下,我想在 docker 构建期间自己设置用户名和密码,这样我就知道创建了哪些用户等(假设这是明智的做法)。
任何提示,我们将不胜感激。
您发布的链接使用标准 Postgres Docker Image。
您可以通过设置环境变量来扩展/配置此映像。
最突出的是:
您发布的
config/docker.env
也使用环境变量 - 但仅用于配置应用程序 - 而不是 Postgres 映像。此设置默认采用 postgres:10-alpine
的默认值。
您可以添加 Postgres 映像可以理解的环境变量并使用现有的应用程序配置。
为了使其能够使用自行选择的用户名和密码。这应该足够了:
config/docker.env
# ...
DATABASE_USER=dbsuperuser
DATABASE_PASS=dbsuperuserpassword
POSTGRES_PASSWORD=dbsuperuserpassword
POSTGRES_USER=dbsuperuser
# ...
警告:环境变量配置的用户和密码是超级用户。这不应该是应用程序连接到数据库的用户 - 尽管本教程提倡这一点。
在 ubuntu 24 上,默认密码不是我的 postgres。我需要在
mix ecto.create
工作之前更新密码。您需要进入 shell,然后:
04:29:52.085 [error] Postgrex.Protocol (#PID<0.470.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
04:29:52.098 [error] Postgrex.Protocol (#PID<0.478.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"
** (Mix) The database for Beefcharts.Repo couldn't be created: killed
root@ubuntu:~/beefcharts.com# psql
psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL: role "root" does not exist
root@ubuntu:~/beefcharts.com# sudo -u postgres psql
psql (16.3 (Ubuntu 16.3-0ubuntu0.24.04.1))
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes
-----------+------------------------------------------------------------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS
postgres=# \password postgres
Enter new password for user "postgres": <-- this is where you update the password
Enter it again:
postgres=# \q
root@ubuntu:~/beefcharts.com# mix ecto.create
The database for Beefcharts.Repo has been created
root@ubuntu:~/beefcharts.com#