gpt4 book ai didi

typescript - 无法使用 TypeORM + pgAdmin4 连接到在 docker 上运行的本地 PostgreSQL 数据库

转载 作者:行者123 更新时间:2023-12-02 19:16:14 36 4
gpt4 key购买 nike

我在本地主机上运行的 docker 中托管了一个 postgres 数据库,默认的 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

因此,我希望能够通过在本地主机上输入默认端口 5432 来通过 pgAdmin4 连接到数据库(也尝试了本地主机 ip 127.0.0.1 )但是当我尝试使用以下登录时凭据(你必须相信我密码是正确输入并重新输入 10 次并复制粘贴的) postgres database login page当我然后按登录时,出现以下错误:

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

最佳答案

正在初始化脚本中设置用户和密码。

如果这不是您第一次对该文件运行组合,那么您很可能已经在容器中安装的卷上拥有数据。在这种情况下,初始化脚本被跳过,您应该在 docker-compose up 输出中看到此日志:

db_1  | PostgreSQL Database directory appears to contain a database; Skipping initialization

这些选项中的任何一个都应该可以解决您的问题:

  • 记住您第一次设置的密码,并使用该密码进行连接

  • 清空该卷或完全删除它(@pacuna 已经在评论中解释了如何做到这一点 - 这将清除数据并从头开始重新初始化一切!)
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 客户端容器化后,在尝试重现时,问题消失了

关于typescript - 无法使用 TypeORM + pgAdmin4 连接到在 docker 上运行的本地 PostgreSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63366952/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com