
我希望從遠端客戶端連接我的 Postgres docker 服務。這就是我創建容器的方式:
sudo docker run --name pg -p 5432:5432 -v pg_data:/var/lib/postgres/data -e POSTGRES_DB=mydb -e POSTGRES_USER=dbowner -e POSTGRES_PASSWORD=MySecretPassword -d postgres -c "listen_addresses='*'"
我做了研究,有人說pg_hba.conf
應該編輯該文件。我應該如何編輯pg_hba.conf?沒有配置參數來設定pg_hba.conf
.另外,host all all all md5
和之間有什麼差別嗎host all all 0.0.0.0/0 md5
?我應該使用哪一個以及如何將其放入pg_hba.conf
docker 容器中?
netstat
這是創建容器後的輸出:
$ netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
tcp6 0 0 :::22 :::* LISTEN
tcp6 0 0 :::5432 :::* LISTEN
tcp6 0 0 :::3000 :::* LISTEN
tcp6 0 0 :::4000 :::* LISTEN
tcp6 0 0 :::4001 :::* LISTEN
tcp6 0 0 :::6379 :::* LISTEN
這是否意味著 PostgreSQL 服務只監聽 IPv6?
答案1
有以下錯誤-v option
:
- 你寫的
/var/lib/postgres/data
應該是/var/lib/postgresql/data
- 主機路徑必須完整:
/root/pgdata
- 如果主機使用 selinux 則使用
-v /root/pgdata:/var/lib/postgresql/data:Z
,如果不使用則:rw
使用
這是完美運行的:
root@testubt:~# docker run --name pg -p 5432:5432 -v /root/pgdata:/var/lib/postgresql/data:Z -e POSTGRES_DB=mydb -e POSTGRES_USER=dbowner -e POSTGRES_PASSWORD=MySecretPassword -d postgres:alpine
root@testubt:~# ss -tnl | grep 5432
LISTEN 0 128 :::5432 :::*
測試本地主機:
root@testubt:~# PGPASSWORD=MySecretPassword psql -U dbowner mydb -h localhost
psql (9.5.14, server 10.5)
Type "help" for help.
mydb=# \q
在其他機器上測試:
alp01:~# PGPASSWORD=MySecretPassword psql -U dbowner mydb -h testubt
psql (10.5)
Type "help" for help.
mydb=#
如果你想編輯pg_hba.conf
它,就在/root/pgdata
目錄中。我相信你不會需要它。
乾杯:)