簡體   English   中英

我無法從 Intellij 連接到 Docker 上的 postgres 數據庫

[英]I can't connect to my postgres database on Docker from Intellij

我正在執行與以下描述相同的步驟: 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

為卷創建目錄:C:\\Users{user}\\docker\\postgres_data\\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 並創建數據庫(僅在空卷上首次創建容器后):

docker ps
  

得到輸出:
容器 ID 圖像命令創建狀態端口名稱 e626f4c7b958 postgres:11.3 "docker-entrypoint.s..." 大約一個小時前 Up 大約一個小時 0.0.0.0:5432->5432/tcp postgres

跑:

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

使用 PASSWORD=root 從 Intellij 連接:

在此處輸入圖片說明

您似乎在 5432 端口上運行了 postgresql 實例。 檢查端口 5432 上運行的是什么:

sudo lsof -i :5432

如果有任何記錄,您可以手動殺死正在使用該端口的進程( man kill )並重新運行 docker 容器。 在我的例子中,這個端口上運行了另一個本地的 postgresql 實例,所以下面的內容對我有幫助:

sudo pkill -u postgres

或者(更可取)為您的 docker postgresql 指定不同的端口映射(例如 5433):

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

嘗試刪除您在本地安裝的 PostgreSQL(如果存在)。 因為我的問題恰恰是這個想法不知道在哪里連接,到 docker survey 或我電腦上安裝的數據庫

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM