Cài đặt PostgreSQL trên VPS Ubuntu và cấu hình chuẩn để chạy ổn định và an toàn

PostgreSQL là lựa chọn rất phổ biến cho web/app backend. Trên VPS Ubuntu, phần “cài” thường nhanh, nhưng phần “cấu hình đúng” (quyền truy cập, remote, firewall, auth, log) mới quyết định hệ thống của bạn có an toàn và dễ vận hành hay không. Bài này hướng dẫn theo kiểu từng bước, dùng được ngay cho môi trường dev lẫn production.

{"type":"doc","content":[{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"I) Chuẩn bị trước khi cài"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1) Cập nhật hệ thống + cài tiện ích cơ bản"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo apt update\nsudo apt -y upgrade\nsudo apt -y install curl ca-certificates gnupg lsb-release\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2) Xác định bạn muốn cài theo “repo Ubuntu” hay “repo PGDG”"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Repo mặc định của Ubuntu"},{"type":"text","text":": dễ, ổn định, phù hợp đa số nhu cầu."}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"Repo PGDG (chính chủ PostgreSQL)"},{"type":"text","text":": chọn được phiên bản mới hơn, cập nhật theo vòng đời PostgreSQL, hữu ích nếu bạn cần major version cụ thể. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/download/linux/ubuntu/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL+1"}]}]}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"II) Cài PostgreSQL trên Ubuntu"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1) Cách A: Cài từ repo mặc định (khuyến nghị nếu bạn không có yêu cầu version)"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo apt -y install postgresql postgresql-contrib\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Ubuntu (và PostgreSQL upstream) đều ghi rõ bạn có thể cài trực tiếp bằng "},{"type":"text","marks":[{"type":"code"}],"text":"apt install postgresql"},{"type":"text","text":". "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"documentation.ubuntu.com+1"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2) Cách B: Cài từ repo PGDG để chọn phiên bản (ví dụ PostgreSQL 18)"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"PostgreSQL upstream có hướng dẫn “manual” và cả script tự động cho PGDG. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/download/linux/ubuntu/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL+1"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"B1. Thêm repo PGDG (manual – rõ ràng, dễ debug)"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo install -d /usr/share/postgresql-common/pgdg\nsudo curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail \\\n https://www.postgresql.org/media/keys/ACCC4CF8.asc\n\n. /etc/os-release\nsudo sh -c \"echo 'deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] \\\nhttps://apt.postgresql.org/pub/repos/apt $VERSION_CODENAME-pgdg main' \\\n> /etc/apt/sources.list.d/pgdg.list\"\n\nsudo apt update\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/download/linux/ubuntu/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL+1"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"bold"}],"text":"B2. Cài phiên bản cụ thể"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo apt -y install postgresql-18\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/download/linux/ubuntu/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"3) Bật service + kiểm tra"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo systemctl enable --now postgresql\nsystemctl status postgresql --no-pager\npsql --version\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"III) Tạo user/database cho ứng dụng (đúng chuẩn tách quyền)"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1) Vào psql bằng user hệ thống "},{"type":"text","marks":[{"type":"code"}],"text":"postgres"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo -iu postgres psql\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2) Tạo role + database riêng cho app"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Trong "},{"type":"text","marks":[{"type":"code"}],"text":"psql"},{"type":"text","text":":"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"CREATE ROLE appuser WITH LOGIN PASSWORD 'CHANGE_ME_STRONG_PASSWORD';\nCREATE DATABASE appdb OWNER appuser;\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Thoát:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"\\q\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"3) Test kết nối local"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"psql \"postgresql://appuser:CHANGE_ME_STRONG_PASSWORD@127.0.0.1:5432/appdb\"\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"IV) Cấu hình truy cập và bảo mật (phần quan trọng nhất)"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Trên Ubuntu, cấu hình PostgreSQL thường nằm ở:"},{"type":"hardBreak"},{"type":"text","marks":[{"type":"code"}],"text":"/etc/postgresql/<version>/main/"},{"type":"text","text":" (gồm "},{"type":"text","marks":[{"type":"code"}],"text":"postgresql.conf"},{"type":"text","text":", "},{"type":"text","marks":[{"type":"code"}],"text":"pg_hba.conf"},{"type":"text","text":", …). "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"documentation.ubuntu.com"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1) "},{"type":"text","marks":[{"type":"code"}],"text":"postgresql.conf"},{"type":"text","text":": bật/tắt remote bằng "},{"type":"text","marks":[{"type":"code"}],"text":"listen_addresses"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Mở file (đổi "},{"type":"text","marks":[{"type":"code"}],"text":"<version>"},{"type":"text","text":" cho đúng, ví dụ 14/16/18):"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo nano /etc/postgresql/<version>/main/postgresql.conf\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":4},"content":[{"type":"text","text":"1.1) Chỉ cho phép local (an toàn nhất nếu app chạy cùng VPS)"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"listen_addresses = 'localhost'\nport = 5432\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":4},"content":[{"type":"text","text":"1.2) Cho phép remote (chỉ khi thật sự cần)"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Tìm dòng "},{"type":"text","marks":[{"type":"code"}],"text":"listen_addresses"},{"type":"text","text":" và đặt:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"listen_addresses = '*'\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Ubuntu docs cũng mô tả rõ cách sửa "},{"type":"text","marks":[{"type":"code"}],"text":"listen_addresses"},{"type":"text","text":" từ localhost sang "},{"type":"text","marks":[{"type":"code"}],"text":"*"},{"type":"text","text":" để cho phép máy khác kết nối. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"documentation.ubuntu.com"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Mẹo: nếu bạn chỉ muốn nghe IPv4, Ubuntu docs gợi ý "},{"type":"text","marks":[{"type":"code"}],"text":"0.0.0.0"},{"type":"text","text":"; IPv6 là "},{"type":"text","marks":[{"type":"code"}],"text":"::"},{"type":"text","text":"; còn "},{"type":"text","marks":[{"type":"code"}],"text":"*"},{"type":"text","text":" là cả hai. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"documentation.ubuntu.com"}]}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2) "},{"type":"text","marks":[{"type":"code"}],"text":"pg_hba.conf"},{"type":"text","text":": whitelist IP + chọn phương thức xác thực"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Mở:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo nano /etc/postgresql/<version>/main/pg_hba.conf\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":4},"content":[{"type":"text","text":"2.1) Gợi ý cấu hình “an toàn theo whitelist IP”"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Ví dụ chỉ cho "},{"type":"text","marks":[{"type":"bold"}],"text":"một IP của server ứng dụng"},{"type":"text","text":" truy cập database:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"# Local connections (giữ mặc định cũng được)\nlocal all all peer\n\n# Remote: chỉ cho appuser vào appdb từ 203.0.113.10\nhost appdb appuser 203.0.113.10/32 scram-sha-256\n"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"code"}],"text":"pg_hba.conf"},{"type":"text","text":" là file điều khiển xác thực theo host, và "},{"type":"text","marks":[{"type":"code"}],"text":"scram-sha-256"},{"type":"text","text":" là một method hợp lệ. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/docs/current/auth-pg-hba-conf.html","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"PostgreSQL docs cũng cảnh báo MD5 đang bị deprecate và có thể bị loại bỏ trong tương lai, nên ưu tiên SCRAM. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/docs/current/auth-pg-hba-conf.html","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Trên Ubuntu, "},{"type":"text","marks":[{"type":"bold"}],"text":"local thường là "},{"type":"text","marks":[{"type":"code"}],"text":"peer"},{"type":"text","text":", còn kết nối TCP/IP (host) mặc định đã chuyển sang "},{"type":"text","marks":[{"type":"code"}],"text":"scram-sha-256"},{"type":"text","text":" từ các bản Ubuntu mới. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"documentation.ubuntu.com"}]}]}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"3) Bật SCRAM cho mật khẩu mới bằng "},{"type":"text","marks":[{"type":"code"}],"text":"password_encryption"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Trong "},{"type":"text","marks":[{"type":"code"}],"text":"postgresql.conf"},{"type":"text","text":":"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"password_encryption = scram-sha-256\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Sau đó "},{"type":"text","marks":[{"type":"bold"}],"text":"đổi lại mật khẩu"},{"type":"text","text":" để được hash theo chuẩn mới:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo -iu postgres psql\n"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"ALTER ROLE appuser WITH PASSWORD 'CHANGE_ME_STRONG_PASSWORD';\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"PostgreSQL giải thích việc password dùng được với auth method nào phụ thuộc "},{"type":"text","marks":[{"type":"code"}],"text":"password_encryption"},{"type":"text","text":" tại thời điểm set password. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/docs/current/auth-password.html?utm_source=chatgpt.com","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"PostgreSQL"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"4) Mở firewall đúng cách bằng UFW (chỉ mở cho IP cần)"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Nếu bạn bật remote, hãy mở port 5432 theo whitelist IP:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo ufw allow from 203.0.113.10 to any port 5432\nsudo ufw status\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"DigitalOcean có ví dụ trực tiếp về rule mở PostgreSQL port 5432 theo IP/subnet. "},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands","target":"_blank","rel":"noopener","class":"flex h-4.5 overflow-hidden rounded-xl px-2 text-[9px] font-medium transition-colors duration-150 ease-in-out text-token-text-secondary! bg-[#F4F4F4]! dark:bg-[#303030]!"}}],"text":"DigitalOcean+1"}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Nếu app và DB cùng VPS: "},{"type":"text","marks":[{"type":"bold"}],"text":"không cần mở 5432 ra Internet"},{"type":"text","text":". Giữ "},{"type":"text","marks":[{"type":"code"}],"text":"listen_addresses = 'localhost'"},{"type":"text","text":" là đủ."}]}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"5) Reload/restart PostgreSQL"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"sudo systemctl reload postgresql\n# nếu có thay đổi yêu cầu restart (hoặc bạn muốn chắc chắn):\nsudo systemctl restart postgresql\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"V) Kết nối từ máy cá nhân mà không mở port 5432 (khuyến nghị khi thao tác admin)"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Nếu bạn chỉ cần psql/DB tool để quản trị, dùng SSH tunnel:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"ssh -L 5432:127.0.0.1:5432 user@IP_VPS\n"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Rồi trên máy bạn:"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"psql \"postgresql://appuser:CHANGE_ME_STRONG_PASSWORD@127.0.0.1:5432/appdb\"\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"VI) Một vài cấu hình vận hành cơ bản nên bật sớm"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"1) Bật log truy vấn chậm để tối ưu sau này"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Trong "},{"type":"text","marks":[{"type":"code"}],"text":"postgresql.conf"},{"type":"text","text":":"}]},{"type":"codeBlock","attrs":{"language":null},"content":[{"type":"text","text":"log_min_duration_statement = 500ms\n"}]},{"type":"heading","attrs":{"textAlign":null,"level":3},"content":[{"type":"text","text":"2) Checklist nhanh trước khi đưa vào production"}]},{"type":"bulletList","content":[{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"App dùng "},{"type":"text","marks":[{"type":"bold"}],"text":"user riêng"},{"type":"text","text":" (không dùng "},{"type":"text","marks":[{"type":"code"}],"text":"postgres"},{"type":"text","text":")"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"code"}],"text":"pg_hba.conf"},{"type":"text","text":" "},{"type":"text","marks":[{"type":"bold"}],"text":"không"},{"type":"text","text":" có rule mở rộng kiểu "},{"type":"text","marks":[{"type":"code"}],"text":"0.0.0.0/0"},{"type":"text","text":" trừ khi bạn có lớp bảo vệ khác"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Firewall chỉ mở 5432 cho IP/subnet cần thiết"}]}]},{"type":"listItem","content":[{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Có backup định kỳ (ít nhất "},{"type":"text","marks":[{"type":"code"}],"text":"pg_dump"},{"type":"text","text":" hằng ngày)"}]}]}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"Kết luận"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","text":"Trên Ubuntu, bạn có thể cài PostgreSQL rất nhanh bằng "},{"type":"text","marks":[{"type":"code"}],"text":"apt"},{"type":"text","text":", nhưng để chạy ổn định và an toàn trên VPS, bạn nên: (1) tách role/database cho ứng dụng, (2) cấu hình "},{"type":"text","marks":[{"type":"code"}],"text":"listen_addresses"},{"type":"text","text":" và "},{"type":"text","marks":[{"type":"code"}],"text":"pg_hba.conf"},{"type":"text","text":" theo whitelist IP, (3) ưu tiên "},{"type":"text","marks":[{"type":"code"}],"text":"scram-sha-256"},{"type":"text","text":", và (4) mở firewall đúng mức cần thiết. Làm đúng các bước này sẽ giúp bạn giảm rủi ro bị scan/brute-force và dễ vận hành lâu dài."}]},{"type":"heading","attrs":{"textAlign":null,"level":2},"content":[{"type":"text","text":"Tài liệu tham khảo"}]},{"type":"paragraph","attrs":{"textAlign":null},"content":[{"type":"text","marks":[{"type":"link","attrs":{"href":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/?utm_source=chatgpt.com","target":"_new","rel":"noopener","class":"decorated-link"}}],"text":"https://documentation.ubuntu.com/server/how-to/databases/install-postgresql/"},{"type":"hardBreak"},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/download/linux/ubuntu/?utm_source=chatgpt.com","target":"_new","rel":"noopener","class":"decorated-link"}}],"text":"https://www.postgresql.org/download/linux/ubuntu/"},{"type":"hardBreak"},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://apt.postgresql.org/?utm_source=chatgpt.com","target":"_new","rel":"noopener","class":"decorated-link"}}],"text":"https://apt.postgresql.org/"},{"type":"hardBreak"},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/docs/current/auth-pg-hba-conf.html?utm_source=chatgpt.com","target":"_new","rel":"noopener","class":"decorated-link"}}],"text":"https://www.postgresql.org/docs/current/auth-pg-hba-conf.html"},{"type":"hardBreak"},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.postgresql.org/docs/current/auth-password.html?utm_source=chatgpt.com","target":"_new","rel":"noopener","class":"decorated-link"}}],"text":"https://www.postgresql.org/docs/current/auth-password.html"},{"type":"hardBreak"},{"type":"text","marks":[{"type":"link","attrs":{"href":"https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands?utm_source=chatgpt.com","target":"_new","rel":"noopener","class":"decorated-link"}}],"text":"https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands"}]}]}