intellij-idea 我无法从Intellij连接到Docker上的postgres数据库

ua4mk5z4  于 2022-11-01  发布在  Docker
关注(0)|答案(2)|浏览(222)

我执行的步骤与中所述的步骤相同:https://medium.com/better-programming/connect-from-local-machine-to-postgresql-docker-container-f785f00461a7
但当我尝试从Intellij连接到我的postgres(使用密码mysecretpassword)时,我得到以下错误:

The specified database user/password combination is rejected: [28P01] FATAL: password authentication failed for user "postgres"

当然,我可以通过cmd命令连接到我的数据库:

$ psql -h localhost -p 5432 -U postgres -W                       Password for user postgres:                       
psql (9.5.5, server 10.3 (Debian 10.3-1.pgdg90+1))                       WARNING: psql major version 9.5, server major version 10.                                Some psql features might not work.                       
Type "help" for help.                                               postgres=# \l

我的容器已启动:

这是怎么回事?我不知道...我在Intellij中使用postgres驱动程序42.2.5
来自容器的日志:

The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Etc/UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
initdb: warning: enabling "trust" authentication for local connections
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.
syncing data to disk ... ok

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....2020-09-30 11:17:42.613 UTC [45] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-09-30 11:17:42.618 UTC [45] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-30 11:17:42.642 UTC [46] LOG:  database system was shut down at 2020-09-30 11:17:42 UTC
2020-09-30 11:17:42.649 UTC [45] LOG:  database system is ready to accept connections
 done
server started

/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

2020-09-30 11:17:42.700 UTC [45] LOG:  received fast shutdown request
waiting for server to shut down....2020-09-30 11:17:42.705 UTC [45] LOG:  aborting any active transactions
2020-09-30 11:17:42.708 UTC [45] LOG:  background worker "logical replication launcher" (PID 52) exited with exit code 1
2020-09-30 11:17:42.708 UTC [47] LOG:  shutting down
2020-09-30 11:17:42.737 UTC [45] LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

2020-09-30 11:17:42.836 UTC [1] LOG:  starting PostgreSQL 12.2 (Debian 12.2-2.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
2020-09-30 11:17:42.836 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2020-09-30 11:17:42.836 UTC [1] LOG:  listening on IPv6 address "::", port 5432
2020-09-30 11:17:42.845 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
2020-09-30 11:17:42.881 UTC [54] LOG:  database system was shut down at 2020-09-30 11:17:42 UTC
2020-09-30 11:17:42.888 UTC [1] LOG:  database system is ready to accept connections
laawzig2

laawzig21#

为卷创建目录:C:\用户{用户}\docker\postgres_data\数据
连接到Windows上的Postgres:

docker run -p 5432:5432 --name postgres -v C:\Users\{user}\docker\postgres_data\data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=root -d postgres:11.3

从CMD连接到Postgres并创建DB(仅在空卷上首次创建容器后):

docker ps

获取输出:
集装箱ID图像命令创建状态端口名称e626 f4 c7 b 958 postgres:11. 3“docker-entrypoint. s”大约一个小时以前截至大约一个小时0. 0. 0. 0:5432-〉5432/tcp postgres
运行时间:

docker exec -it {CONTAINER ID} bash
psql -h localhost -U postgres
psql -U postgres

使用密码=root从Intellij连接:

nkhmeac6

nkhmeac62#

您似乎在5432端口上运行了postgresql示例。请检查端口5432上运行的是什么:

sudo lsof -i :5432

如果有任何记录,您可以手动终止正在使用此端口(man kill)的进程,并重新运行docker container。在我的示例中,有另一个postgresql本地示例正在此端口上运行,因此以下内容对我有帮助:

sudo pkill -u postgres

或者(更可取的做法是)为您的docker postgresql指定一个不同的端口Map(例如5433):

docker run -p 5433:5432 ...other_your_flags... postgres:latest

相关问题