POSTGRES_PASSWORD
环境变量才能使用密码创建数据库。node-postgres
需要PGPassword. 都需要是相同的密码。 我正在使用.env.local加载密码和.env用于不需要安全存储并且可以包含在源代码中的常见环境变量。 我的.env文件的关键:
DB_USER=root
# DB_PASSWORD=<PASSWORD>
DB_DBNAME=qr_orders_db
PGUSER=${DB_USER}
PGPASSWORD=${DB_PASSWORD}
PGDATABASE=${DB_DBNAME}
POSTGRES_USER=${DB_USER}
POSTGRES_PASSWORD=${DB_PASSWORD}
POSTGRES_DB=${PGDATABASE}
DB_PASSWORD=root
MyDocker撰写文件:
services:
db:
image: postgres
env_file:
- .env
- .env.local
volumes:
- pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
pgdata:
我运行时输出错误:
:docker compose up
为什么它不起作用?如果我在.env之前将.env.local放置,我不会收到错误消息,但是警告持续出现:
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
[+] Running 3/3
✔ Network qr_orders_default Created 0.1s
✔ Volume "qr_orders_pgdata" Created 0.0s
✔ Container qr_orders-db-1 Created 0.1s
Attaching to db-1
db-1 | Error: Database is uninitialized and superuser password is not specified.
db-1 | You must specify POSTGRES_PASSWORD to a non-empty value for the
db-1 | superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
db-1 |
db-1 | You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
db-1 | connections without a password. This is *not* recommended.
db-1 |
db-1 | See PostgreSQL documentation about "trust":
db-1 | https://www.postgresql.org/docs/current/auth-trust.html
db-1 exited with code 1
我做了一个一些实验,以确认未设置密码,但显然是在设置密码。我不知道为什么我会收到警告消息。
实验:
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
WARN[0000] The "DB_PASSWORD" variable is not set. Defaulting to a blank string.
[+] Running 3/3
✔ Network qr_orders_default Created 0.1s
✔ Volume "qr_orders_pgdata" Created 0.0s
✔ Container qr_orders-db-1 Created 0.1s
Attaching to db-1
db-1 | The files belonging to this database system will be owned by user "postgres".
db-1 | This user must also own the server process.
db-1 |
db-1 | The database cluster will be initialized with locale "en_US.utf8".
db-1 | The default database encoding has accordingly been set to "UTF8".
db-1 | The default text search configuration will be set to "english".
db-1 |
db-1 | Data page checksums are disabled.
db-1 |
db-1 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db-1 | creating subdirectories ... ok
db-1 | selecting dynamic shared memory implementation ... posix
db-1 | selecting default "max_connections" ... 100
db-1 | selecting default "shared_buffers" ... 128MB
db-1 | selecting default time zone ... Etc/UTC
db-1 | creating configuration files ... ok
db-1 | running bootstrap script ... ok
db-1 | performing post-bootstrap initialization ... ok
db-1 | initdb: warning: enabling "trust" authentication for local connections
db-1 | initdb: hint: 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.
db-1 | syncing data to disk ... ok
db-1 |
db-1 |
db-1 | Success. You can now start the database server using:
db-1 |
db-1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
db-1 |
db-1 | waiting for server to start....2025-01-28 14:28:10.701 UTC [48] LOG: starting PostgreSQL 17.2 (Debian 17.2-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db-1 | 2025-01-28 14:28:10.703 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2025-01-28 14:28:10.710 UTC [51] LOG: database system was shut down at 2025-01-28 14:28:10 UTC
db-1 | 2025-01-28 14:28:10.717 UTC [48] LOG: database system is ready to accept connections
db-1 | done
db-1 | server started
db-1 | CREATE DATABASE
db-1 |
db-1 |
db-1 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db-1 |
db-1 | 2025-01-28 14:28:10.928 UTC [48] LOG: received fast shutdown request
db-1 | waiting for server to shut down....2025-01-28 14:28:10.930 UTC [48] LOG: aborting any active transactions
db-1 | 2025-01-28 14:28:10.934 UTC [48] LOG: background worker "logical replication launcher" (PID 54) exited with exit code 1
db-1 | 2025-01-28 14:28:10.935 UTC [49] LOG: shutting down
db-1 | 2025-01-28 14:28:10.937 UTC [49] LOG: checkpoint starting: shutdown immediate
db-1 | 2025-01-28 14:28:10.981 UTC [49] LOG: checkpoint complete: wrote 921 buffers (5.6%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.023 s, sync=0.015 s, total=0.046 s; sync files=301, longest=0.002 s, average=0.001 s; distance=4238 kB, estimate=4238 kB; lsn=0/1908978, redo lsn=0/1908978
db-1 | 2025-01-28 14:28:10.989 UTC [48] LOG: database system is shut down
db-1 | done
db-1 | server stopped
db-1 |
db-1 | PostgreSQL init process complete; ready for start up.
db-1 |
db-1 | 2025-01-28 14:28:11.064 UTC [1] LOG: starting PostgreSQL 17.2 (Debian 17.2-1.pgdg120+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit
db-1 | 2025-01-28 14:28:11.065 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db-1 | 2025-01-28 14:28:11.065 UTC [1] LOG: listening on IPv6 address "::", port 5432
db-1 | 2025-01-28 14:28:11.068 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db-1 | 2025-01-28 14:28:11.075 UTC [64] LOG: database system was shut down at 2025-01-28 14:28:10 UTC
db-1 | 2025-01-28 14:28:11.083 UTC [1] LOG: database system is ready to accept connections
明显地,当.env.local在docker组合文件中加载之前,这不会引发错误。生活之谜,我猜_(ツ)_/。我现在不会将自己的回答标记为被接受的答案,因为我想看看是否有人知道如何摆脱该警告。