PostgreSQL 遠端連線
本篇是記錄怎麼遠端連線到 PostgreSQL 服務。
運行環境
環境都是在 「Proxmox VE 」 虛擬系統上架設,都是以 「 LXC 」模式為主,除非有特殊狀況會告知使用 「 VM 」 模式
- 系統環境: Debian 13
- 資料庫服務: PostgreSQL 18
設定過程
- 如何連線到
PostgreSQL服務,那要先切換到PostgreSQL使用者讓這個帳號修改密碼
passwd postgres
- 修改完後切換到
postgres
sudo su postgres
- 連入之後在去資料庫做連線
psql
- 連入到資料庫後修改管理者的密碼
ALTER ROLE postgres WITH PASSWORD 'mypassword';
-
修改完之後再啟用
pgadmin4做遠端連線做記錄一下。 -
先進到 PostgreSQL 目錄
cd /etc/postgresql/18/main/
- 編輯
vim pg_hba.conf設定檔
# 修改前
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# 修改後
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
- 編輯
vim postgresql.conf設定檔,把localhost這個參數改成*即可
# 修改前
listen_addresses = 'localhost' # what IP address(es) to listen on;
# 修改後
listen_addresses = '*' # what IP address(es) to listen on;
- 修改完設定後重啟 postgresql 服務,最後再遠端連線測試看是否都正常連線。
sudo systemctl restart postgresql