[{"data":1,"prerenderedAt":331},["ShallowReactive",2],{"content:\u002Fposts\u002Fpostgresql-install":3,"surround:\u002Fposts\u002Fpostgresql-install":321},{"id":4,"title":5,"body":6,"categories":293,"date":295,"description":296,"draft":297,"extension":298,"image":299,"meta":300,"navigation":302,"path":303,"permalink":299,"pinned":297,"published":299,"readingTime":304,"recommend":299,"references":299,"seo":309,"sitemap":310,"stem":311,"tags":312,"type":318,"updated":319,"__hash__":320},"content\u002Fposts\u002Fposts\u002Fpostgresql-install.md","PostgreSQL 实战指南：安装、配置、备份与跨库查询",{"type":7,"value":8,"toc":267},"minimark",[9,19,24,35,39,45,53,57,60,69,73,76,94,100,104,111,119,127,130,136,142,145,151,154,159,165,169,172,178,181,187,192,198,202,210,213,219,222,228,231,237,240,246,255,258,261],[10,11,15],"alert",{"type":12,"icon":13,"title":14},"info","tabler:robot","AI 迁移提示",[16,17,18],"p",{},"本文由 AI 协助从旧站迁移，尚未完成逐篇人工审校；内容如有疏漏，将在复核后修订。",[20,21,23],"h2",{"id":22},"一ubuntu-二进制安装","一、Ubuntu 二进制安装",[25,26,32],"pre",{"className":27,"code":29,"language":30,"meta":31},[28],"language-bash","# 安装 PostgreSQL 14\napt install -y postgresql-14\n\n# 查看服务状态\nsystemctl status postgresql\n","bash","",[33,34,29],"code",{"__ignoreMap":31},[36,37,38],"h3",{"id":38},"修改数据库密码",[25,40,43],{"className":41,"code":42,"language":30,"meta":31},[28],"# 切换到 postgres 用户并登录\nsu - postgres\npsql\n\n# 或直接以 root 身份登录\npsql -U postgres\n",[33,44,42],{"__ignoreMap":31},[25,46,51],{"className":47,"code":49,"language":50,"meta":31},[48],"language-sql","-- 修改密码\nALTER USER postgres WITH PASSWORD 'your-password';\n","sql",[33,52,49],{"__ignoreMap":31},[20,54,56],{"id":55},"二docker-compose-部署","二、Docker Compose 部署",[16,58,59],{},"适合测试环境，可在一台主机运行多个 PostgreSQL 实例：",[25,61,67],{"className":62,"code":64,"filename":65,"language":66,"meta":31},[63],"language-yaml","services:\n  pgsql:\n    image: postgres:14\n    container_name: postgres\n    restart: always\n    command: >\n      postgres\n      -c config_file=\u002Fetc\u002Fpostgresql\u002Fpostgresql.conf\n      -c hba_file=\u002Fetc\u002Fpostgresql\u002Fpg_hba.conf\n    environment:\n      POSTGRES_PASSWORD: Passwd@2026\n      TZ: Asia\u002FShanghai\n    ports:\n      - \"5432:5432\"\n    volumes:\n      - \u002Fopt\u002Fpgsql\u002Fdata:\u002Fvar\u002Flib\u002Fpostgresql\u002Fdata\n      - \u002Fopt\u002Fpgsql\u002Fconfig\u002Fpostgresql.conf:\u002Fetc\u002Fpostgresql\u002Fpostgresql.conf:ro\n      - \u002Fopt\u002Fpgsql\u002Fconfig\u002Fpg_hba.conf:\u002Fetc\u002Fpostgresql\u002Fpg_hba.conf:ro\n    networks:\n      - app-net\n\nnetworks:\n  app-net:\n    external: true\n","docker-compose.yml","yaml",[33,68,64],{"__ignoreMap":31},[20,70,72],{"id":71},"三远程访问与数据目录迁移","三、远程访问与数据目录迁移",[36,74,75],{"id":75},"修改认证方式",[10,77,79],{"type":78},"warning",[16,80,81,82,85,86,89,90,93],{},"以下操作会降低数据库安全性。",[33,83,84],{"code":84},"trust"," 认证允许本地用户免密连接为任意数据库用户，仅建议在开发\u002F测试环境使用。生产环境应保持 ",[33,87,88],{"code":88},"peer"," 或使用 ",[33,91,92],{"code":92},"scram-sha-256","。",[25,95,98],{"className":96,"code":97,"language":30,"meta":31},[28],"# 将 host 连接的认证方式从 ident 改为 md5（密码认证）\nsudo sed -i '\u002F^host\u002Fs\u002Fident\u002Fmd5\u002F' \u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpg_hba.conf\n\n# 将本地连接的认证方式从 peer 改为 trust\nsudo sed -i '\u002F^local\u002Fs\u002Fpeer\u002Ftrust\u002F' \u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpg_hba.conf\n",[33,99,97],{"__ignoreMap":31},[36,101,103],{"id":102},"允许指定-ip-连接","允许指定 IP 连接",[16,105,106,107,110],{},"编辑 ",[33,108,109],{"code":109},"\u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpg_hba.conf","，添加：",[10,112,113],{"type":78},[16,114,115,118],{},[33,116,117],{"code":117},"0.0.0.0\u002F0"," 表示允许所有 IP 连接，生产环境应限制为特定 IP 段，并确保使用强密码。",[25,120,125],{"className":121,"code":123,"language":124},[122],"language-text","# 允许所有 IP 使用密码连接\nhost   all   all   0.0.0.0\u002F0   md5\n","text",[33,126,123],{"__ignoreMap":31},[36,128,129],{"id":129},"监听所有网络接口",[16,131,106,132,135],{},[33,133,134],{"code":134},"\u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpostgresql.conf","，取消注释并修改：",[25,137,140],{"className":138,"code":139,"language":124},[122],"listen_addresses = '*'\n",[33,141,139],{"__ignoreMap":31},[16,143,144],{},"重启并设置服务开机自启：",[25,146,149],{"className":147,"code":148,"language":30,"meta":31},[28],"sudo systemctl restart postgresql\nsudo systemctl enable postgresql\n",[33,150,148],{"__ignoreMap":31},[36,152,153],{"id":153},"数据目录迁移",[10,155,156],{"type":78},[16,157,158],{},"数据目录迁移操作风险较高，权限设置不正确或拷贝不完整会导致 PostgreSQL 无法启动。操作前务必停止服务并做好备份。",[25,160,163],{"className":161,"code":162,"language":30,"meta":31},[28],"# 1. 停止服务\nsystemctl stop postgresql\n\n# 2. 拷贝数据到新目录\ncp -rf \u002Fvar\u002Flib\u002Fpostgresql\u002F14\u002Fmain \u002Fopt\u002Fpostgresql\u002F\n\n# 3. 设置权限\nchown -R postgres:postgres \u002Fopt\u002Fpostgresql\u002F\nchmod 700 \u002Fopt\u002Fpostgresql\u002F\n\n# 4. 修改配置文件中的数据目录\nvim \u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpostgresql.conf\n# 修改：data_directory = '\u002Fopt\u002Fpostgresql\u002Fmain'\n\n# 5. 启动服务\nsystemctl start postgresql\n\n# 6. 验证\npsql -U postgres -c 'SHOW data_directory;'\n",[33,164,162],{"__ignoreMap":31},[20,166,168],{"id":167},"四备份与恢复","四、备份与恢复",[36,170,171],{"id":171},"备份与恢复整个数据库",[25,173,176],{"className":174,"code":175,"language":30,"meta":31},[28],"# 导出整个集群（所有数据库）\nsudo -u postgres pg_dumpall -h localhost -p 5432 -v > \u002Ftmp\u002Fpg_full_backup.sql\n\n# 传输到目标服务器\nrsync -avz --progress \u002Ftmp\u002Fpg_full_backup.sql user@192.168.1.100:\u002Ftmp\u002F\n\n# 实体 PostgreSQL 恢复\nsudo -u postgres psql -f \u002Ftmp\u002Fpg_full_backup.sql\n\n# Docker 容器 PostgreSQL 恢复\ndocker cp \u002Ftmp\u002Fpg_full_backup.sql postgres:\u002Ftmp\u002F\ndocker exec -i postgres psql -U postgres -f \u002Ftmp\u002Fpg_full_backup.sql\n",[33,177,175],{"__ignoreMap":31},[36,179,180],{"id":180},"备份与恢复单个表",[25,182,185],{"className":183,"code":184,"language":30,"meta":31},[28],"# 备份指定表\nsudo -u postgres pg_dump -h localhost -p 5432 \\\n  -d mydb -t users -v > \u002Ftmp\u002Fusers_backup.sql\n\n# 备份时包含 DROP\u002FCREATE 语句（便于重复恢复）\nsudo -u postgres pg_dump -h localhost -p 5432 \\\n  -d mydb -t users --clean --if-exists -v > \u002Ftmp\u002Fusers_backup_clean.sql\n\n# 传输到目标服务器\nrsync -avz --progress \u002Ftmp\u002Fusers_backup.sql user@192.168.1.100:\u002Ftmp\u002F\n",[33,186,184],{"__ignoreMap":31},[10,188,189],{"type":12},[16,190,191],{},"如果目标表已存在，需要先删除或重命名，否则恢复会报错。",[25,193,196],{"className":194,"code":195,"language":30,"meta":31},[28],"# 实体 PostgreSQL 恢复\nsudo -u postgres psql -d target_database -f \u002Ftmp\u002Fusers_backup.sql\n\n# Docker 容器 PostgreSQL 恢复\ndocker cp \u002Ftmp\u002Fusers_backup.sql postgres:\u002Ftmp\u002F\ndocker exec -i postgres psql -U postgres -d target_database -f \u002Ftmp\u002Fusers_backup.sql\n",[33,197,195],{"__ignoreMap":31},[20,199,201],{"id":200},"五fdw-跨库查询","五、FDW 跨库查询",[16,203,204,205,209],{},"PostgreSQL 不支持跨库查询，可通过 ",[206,207,208],"strong",{},"Foreign Data Wrapper（FDW）"," 访问外部数据库的表，实现类似跨库查询的效果。",[36,211,212],{"id":212},"安装扩展",[25,214,217],{"className":215,"code":216,"language":50,"meta":31},[48],"-- 在本地数据库执行\nCREATE EXTENSION IF NOT EXISTS postgres_fdw;\n",[33,218,216],{"__ignoreMap":31},[36,220,221],{"id":221},"创建远程服务器",[25,223,226],{"className":224,"code":225,"language":50,"meta":31},[48],"CREATE SERVER remote_server\nFOREIGN DATA WRAPPER postgres_fdw\nOPTIONS (\n  host '192.168.1.100',\n  dbname 'target_db',\n  port '5432'\n);\n",[33,227,225],{"__ignoreMap":31},[36,229,230],{"id":230},"创建用户映射",[25,232,235],{"className":233,"code":234,"language":50,"meta":31},[48],"CREATE USER MAPPING FOR postgres\nSERVER remote_server\nOPTIONS (\n  user 'postgres',\n  password 'remote-password'\n);\n",[33,236,234],{"__ignoreMap":31},[36,238,239],{"id":239},"导入远程表",[25,241,244],{"className":242,"code":243,"language":50,"meta":31},[48],"-- 导入单个表\nIMPORT FOREIGN SCHEMA public\nLIMIT TO (table_name)\nFROM SERVER remote_server\nINTO public;\n\n-- 导入多个表\nIMPORT FOREIGN SCHEMA public\nLIMIT TO (table1, table2, table3)\nFROM SERVER remote_server\nINTO public;\n\n-- 导入整个 schema（慎用）\nIMPORT FOREIGN SCHEMA public\nFROM SERVER remote_server\nINTO public;\n",[33,245,243],{"__ignoreMap":31},[10,247,248],{"type":78},[16,249,250,251,254],{},"导入整个 schema 可能与本地表名冲突，建议使用 ",[33,252,253],{"code":253},"LIMIT TO"," 指定需要的表。",[16,256,257],{},"导入后即可像本地表一样使用。",[36,259,260],{"id":260},"管理命令",[25,262,265],{"className":263,"code":264,"language":50,"meta":31},[48],"-- 查看所有外部服务器\nSELECT * FROM pg_foreign_server;\n\n-- 查看导入的外部表\nSELECT * FROM information_schema.foreign_tables;\n\n-- 修改服务器连接信息\nALTER SERVER remote_server\nOPTIONS (SET host '192.168.1.200', SET port '5432', SET dbname 'new_db');\n\n-- 修改用户映射密码\nALTER USER MAPPING FOR CURRENT_USER\nSERVER remote_server\nOPTIONS (SET password 'new-password');\n\n-- 增加批量获取行数（优化性能）\nALTER SERVER remote_server OPTIONS (ADD fetch_size '50000');\n\n-- 删除外部表\nDROP FOREIGN TABLE table_name;\n\n-- 删除用户映射\nDROP USER MAPPING FOR postgres SERVER remote_server;\n\n-- 删除服务器（CASCADE 同时删除依赖的外部表）\nDROP SERVER remote_server CASCADE;\n\n-- 删除扩展\nDROP EXTENSION postgres_fdw;\n",[33,266,264],{"__ignoreMap":31},{"title":31,"searchDepth":268,"depth":268,"links":269},4,[270,275,276,282,286],{"id":22,"depth":271,"text":23,"children":272},2,[273],{"id":38,"depth":274,"text":38},3,{"id":55,"depth":271,"text":56},{"id":71,"depth":271,"text":72,"children":277},[278,279,280,281],{"id":75,"depth":274,"text":75},{"id":102,"depth":274,"text":103},{"id":129,"depth":274,"text":129},{"id":153,"depth":274,"text":153},{"id":167,"depth":271,"text":168,"children":283},[284,285],{"id":171,"depth":274,"text":171},{"id":180,"depth":274,"text":180},{"id":200,"depth":271,"text":201,"children":287},[288,289,290,291,292],{"id":212,"depth":274,"text":212},{"id":221,"depth":274,"text":221},{"id":230,"depth":274,"text":230},{"id":239,"depth":274,"text":239},{"id":260,"depth":274,"text":260},[294],"服务","2026-01-24","介绍 PostgreSQL 的 Ubuntu\u002FDocker 部署、远程访问和数据目录迁移、备份恢复与 FDW 跨库查询配置。",false,"md",null,{"slots":301},{},true,"\u002Fposts\u002Fpostgresql-install",{"text":305,"minutes":306,"time":307,"words":308},"6 min read",5.33,319800,1066,{"title":5,"description":296},{"loc":303},"posts\u002Fposts\u002Fpostgresql-install",[313,314,315,316,317],"PostgreSQL","数据库","Docker","备份","FDW","tech","2026-01-27","dhbC1d8wcVj1ADy5B_w3uUmZFlhalXc3GCkjmKf8Pzs",[322,327],{"title":323,"path":324,"stem":325,"date":326,"type":318,"children":-1},"Nacos 安装与鉴权配置指南","\u002Fposts\u002Fnacos-guide","posts\u002Fposts\u002Fnacos-guide","2026-01-22",{"title":328,"path":329,"stem":330,"date":295,"type":318,"children":-1},"自建Umami统计","\u002Fposts\u002Fumami-config","posts\u002Fposts\u002Fumami-config",1788712224850]