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

如何解决无法使用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次并复制粘贴)

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,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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-