如何解决无法使用TypeORM + pgAdmin4连接到在docker上运行的本地PostgreSQL数据库
我在本地主机上运行的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
因此,我希望能够通过输入localhost上的默认端口5432(也尝试使用localhost ip 127.0.0.1
)通过pgAdmin4连接到数据库,但是当我尝试使用以下凭据登录时(您必须相信我,密码输入正确并输入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,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
以下任何选项都可以解决您的问题:
- 记住您组成时第一次设置的密码并使用该密码进行连接
OR
- 清空该卷或将其完全删除(@pacuna已在注释中说明了如何进行此操作-这将清除数据并重新初始化所有内容!)
docker-compose down -v
OR
- 重置管理员密码:
// 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客户端容器化后,尝试重现该问题,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。