以下是Ubuntu PostgreSQL常見問題及解決方案:
sudo systemctl stop postgresql
sudo -u postgres psql --single -d postgres
ALTER USER postgres WITH PASSWORD 'new_password'; \q
sudo systemctl start postgresql
sudo systemctl status postgresql,確保服務已啟動。postgresql.conf,確認 listen_addresses = '*'(允許所有IP連接)。pg_hba.conf,添加遠程訪問規則(如 host all all 0.0.0.0/0 scram-sha-256)。sudo ufw allow 5432/tcp。sudo netstat -tulnp | grep 5432 查看,若沖突則修改PostgreSQL端口或停止沖突服務。sudo rm /var/run/postgresql/postmaster.pid,然后重啟服務。sudo apt install libpq-dev。shared_buffers(建議設為內存1/4)、work_mem(影響排序效率)。VACUUM 和 ANALYZE 優化表,通過 pg_stat_statements 分析慢查詢。postgresql.conf 中 listen_addresses 包含遠程IP或'*'。pg_hba.conf 中添加遠程訪問規則(如 host all all 0.0.0.0/0 md5)。sudo tail -n 50 /var/log/postgresql/postgresql-<版本>-main.log,用于定位啟動失敗或連接問題。參考來源: