PostgreSQL Setup
验证:未记录
- 适用版本
- 未记录
- 验证范围
- 尚未登记验证记录。
本文缩写(英文全称与中文含义)
| 缩写 | 英文全称/名称 | 中文含义 |
|---|---|---|
| DB | Database | 数据库 |
| HA | High Availability | 高可用 |
| PITR | Point-In-Time Recovery | 时间点恢复 |
| SRE | Site Reliability Engineering | 站点可靠性工程 |
| VM | Virtual Machine | 虚拟机 |
| WAL | Write-Ahead Log | 预写日志 |
Links#
https://www.postgresql.org/download/linux/
https://www.postgresql.org/docs/current/app-initdb.html
https://www.postgresql.org/docs/current/server-start.html
https://www.postgresql.org/docs/current/runtime-config-connection.html
https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
https://github.com/bitnami/charts/tree/main/bitnami/postgresql
https://github.com/zalando/postgres-operator
https://github.com/cloudnative-pg/cloudnative-pg1. Linux VM With systemd#
install packages#
# Debian / Ubuntu example
sudo apt-get update
sudo apt-get install -y postgresql postgresql-contrib单容器 PostgreSQL 适合开发,不是生产 HA;major 升级要先验证扩展、数据迁移和恢复路径。
production note:
prefer PostgreSQL official repository when you need a specific major version
pin major version for production
test major upgrade before changing packagesdirectories#
common directories:
data:
/var/lib/postgresql/<major>/main
config:
/etc/postgresql/<major>/main/postgresql.conf
/etc/postgresql/<major>/main/pg_hba.conf
log:
depends on distro / journald configservice#
sudo systemctl enable postgresql
sudo systemctl start postgresql
sudo systemctl status postgresqlcreate roles and database#
sudo -u postgres psqlCREATE ROLE dba_order LOGIN PASSWORD 'change-me';
CREATE ROLE order_app LOGIN PASSWORD 'change-me';
CREATE DATABASE orders
OWNER dba_order
ENCODING 'UTF8'
TABLESPACE pg_default
CONNECTION LIMIT 200;logs#
journalctl -u postgresql -n 200 --no-pager2. Docker#
local dev#
docker run --rm --name pg-dev -e POSTGRES_DB=orders -e POSTGRES_USER=dba_order -e POSTGRES_PASSWORD=dba_password -p 5432:5432 -v pg-dev-data:/var/lib/postgresql/data postgres:16compose#
services:
postgres:
image: postgres:16
environment:
POSTGRES_DB: orders
POSTGRES_USER: dba_order
POSTGRES_PASSWORD: dba_password
ports:
- "5432:5432"
volumes:
- pg-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U dba_order -d orders"]
interval: 10s
timeout: 5s
retries: 5
volumes:
pg-data:docker notes:
good for local dev
do not use simple single-container setup as production HA
do not bake password into image
mount persistent volume3. K8S With Helm / Operator#
helm chart#
Bitnami PostgreSQL chart:
useful for dev / small internal setup
production usage needs careful review:
backup
replication
security context
persistence
upgrade behavior
monitoringhelm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm install orders-postgres bitnami/postgresql --set auth.database=orders --set auth.username=order_app --set auth.password=change-me --set primary.persistence.size=100Gioperator#
production on Kubernetes:
prefer PostgreSQL operator when DB must run in cluster
common options:
CloudNativePG
Zalando Postgres Operator
operator should handle:
cluster bootstrap
replication
backup
failover
rolling restart
major upgrade workflowtradeoff:
managed cloud PostgreSQL is usually simpler operationally
in-cluster PostgreSQL requires strong storage / backup / SRE maturity4. High Availability#
HA baseline:
primary + at least one standby
streaming replication
WAL archive if PITR required
automatic failover tool / managed service failover
client connection routing after failoverbackup is not HA:
backup restores data after loss
HA keeps service available during node failure
you need bothfailure scenarios:
primary down
standby lag too high
disk full
split brain in self-managed failover
application keeps connecting to old primary5. Operations#
useful commands#
psql "postgresql://dba_order@localhost:5432/orders"
pg_isready -h localhost -p 5432SELECT version();
SELECT current_database();
SELECT current_user;
SELECT pid, state, wait_event_type, wait_event, query
FROM pg_stat_activity
WHERE datname = current_database();reload config#
sudo systemctl reload postgresqlSELECT pg_reload_conf();backup smoke test#
pg_dump --format=custom --file=orders.dump "postgresql://dba_order@localhost:5432/orders"
createdb orders_restore
pg_restore --dbname=orders_restore orders.dumpoperations checklist:
backup can restore
monitor disk usage
monitor replication lag
review long transactions
test config reload in staging
document major upgrade path完整生产实操#
部署、权限、故障与独立恢复提供本模块选定路径的分步实现和验收,补充本页安装参考。