[英]Unable to connect to a local PostgreSQL database running on docker with TypeORM + pgAdmin4
我有一個托管在 docker 中的 postgres 數據庫,該數據庫在 localhost 上運行,默認 postgres 端口為5432
,但我根本無法通過 pgAdmin4 或 TypeORM 連接到數據庫。 兩者都拋出相同的錯誤,說密碼無效。
我正在使用以下docker-compose.yml
文件來創建數據庫:
version: '3.8'
services:
db:
image: postgres
volumes:
- ./pgdata:/var/lib/postgresql/data
ports:
- '5432:5432'
environment:
POSTGRES_DB: maindb
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
因此,我希望能夠通過 pgAdmin4 通過在 localhost 上輸入默認端口 5432 連接到數據庫(也嘗試過 localhost ip 127.0.0.1
)但是當我嘗試使用以下憑據登錄時(您必須相信我的密碼輸入正確並重新輸入 10 次並復制粘貼) 當我然后按登錄時,我收到以下錯誤:
Unable to connect to server:
FATAL: password authentication failed for user "admin"
這真的很奇怪,因為我看不出它為什么會失敗。 我 100% 確定在 docker 桌面上傳遞了環境變量,我可以看到它們是相同的。
我還嘗試通過以下方式通過 TypeORM 連接到數據庫:
createConnection({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'admin',
password: 'admin',
database: 'maindb',
entities: [App],
synchronize: true,
logging: false,
})
.then(connection => console.log('Logged in'))
.catch(error => console.log(error));
我收到此錯誤消息:
error: password authentication failed for user "admin"
REMOVED PATHS FOR CONCISENESS
{
length: 97,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '285',
routine: 'auth_failed'
}
我也看不出這應該失敗的原因。 請問是PostgreSQL問題還是docker問題? 我可能更容易切換數據庫提供商?
編輯 1
以下是啟動時的日志:
Attaching to backend_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 | You can change this by editing pg_hba.conf or using the option -A, or
db_1 | --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....2020-08-12 09:26:47.682 UTC [48] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-12 09:26:47.685 UTC [48] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-12 09:26:47.744 UTC [49] LOG: database system was shut down at 2020-08-12 09:26:44 UTC
db_1 | 2020-08-12 09:26:47.770 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 | 2020-08-12 09:26:50.836 UTC [48] LOG: received fast shutdown request
db_1 | waiting for server to shut down....2020-08-12 09:26:50.847 UTC [48] LOG: aborting any active transactions
db_1 | 2020-08-12 09:26:50.849 UTC [48] LOG: background worker "logical replication launcher" (PID 55) exited with exit code 1
db_1 | 2020-08-12 09:26:50.850 UTC [50] LOG: shutting down
db_1 | 2020-08-12 09:26:50.929 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 | 2020-08-12 09:26:50.971 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1 | 2020-08-12 09:26:50.984 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
db_1 | 2020-08-12 09:26:50.984 UTC [1] LOG: listening on IPv6 address "::", port 5432
db_1 | 2020-08-12 09:26:50.997 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1 | 2020-08-12 09:26:51.051 UTC [66] LOG: database system was shut down at 2020-08-12 09:26:50 UTC
db_1 | 2020-08-12 09:26:51.080 UTC [1] LOG: database system is ready to accept connections
正在初始化腳本中設置用戶和密碼。
如果這不是您第一次在該文件上運行組合,則很可能您已經在容器中安裝的卷上有數據。 在這種情況下,初始化腳本被跳過,您應該在 output 上的 docker-compose 上看到此日志:
db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization
這些選項中的任何一個都可以解決您的問題:
或者
docker-compose down -v
或者
// Open a console in the container
docker exec -ti <your-psql-container-id> sh
// Connect to your maindb using the admin user
psql -d maindb -U admin
// Change the admin user password
ALTER USER admin WITH ENCRYPTED PASSWORD 'admin-new-pass';
...然后再次嘗試使用admin-new-pass進行連接
故障排除后編輯:
該問題是由 pgAdmin4 客戶端的錯誤配置引起的。 在容器化 pgAdmin4 客戶端后,在嘗試重現時,問題消失了
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.