导出数据
导出所有数据
1
pg_dumpall -h localhost -p 5432 -U postgres -W -f alldata.sql
导入数据
1
psql -h localhost -p 5432 -U postgres -W -f alldata.sql
导出整个数据库(结构+数据)
1
2
3
4
5
6
7
8
# 基础命令
pg_dump -U <用户名> -h <主机> -d <数据库名> -f <输出文件.sql>
# 示例(导出到当前目录)
pg_dump -U postgres -h localhost -d mydb -f mydb_full_backup.sql
# Kubernetes 环境(进入 PostgreSQL Pod 执行)
kubectl exec -it postgres-pod -- pg_dump -U postgres mydb > mydb.sql
仅导出表结构(无数据)
1
pg_dump -U postgres -h localhost -d mydb --schema-only -f schema.sql
仅导出数据(不包含表结构)
1
pg_dump -U postgres -h localhost -d mydb --data-only -f data.sql
导出指定表
导出单个表(结构+数据)
1
2
3
4
pg_dump -U postgres -h localhost -d mydb -t <表名> -f table.sql
# 示例(导出 users 表)
pg_dump -U postgres -h localhost -d mydb -t users -f users.sql
导出多个表
1
2
pg_dump -U postgres -h localhost -d mydb \
-t table1 -t table2 -f tables.sql
使用通配符导出表
1
2
3
# 导出所有以 "log_" 开头的表
pg_dump -U postgres -h localhost -d mydb \
-t 'log_*' -f log_tables.sql
本文由作者按照
CC BY 4.0
进行授权