[{"data":1,"prerenderedAt":184},["ShallowReactive",2],{"content:\u002Fposts\u002Fpostgresql-config":3,"surround:\u002Fposts\u002Fpostgresql-config":173},{"id":4,"title":5,"body":6,"categories":149,"date":151,"description":152,"draft":153,"extension":154,"image":155,"meta":156,"navigation":158,"path":159,"permalink":155,"pinned":153,"published":155,"readingTime":160,"recommend":155,"references":155,"seo":165,"sitemap":166,"stem":167,"tags":168,"type":171,"updated":151,"__hash__":172},"content\u002Fposts\u002Fposts\u002Fpostgresql-config.md","PostgreSQL 远程访问与数据目录迁移",{"type":7,"value":8,"toc":137},"minimark",[9,19,30,35,40,59,69,73,80,88,96,100,106,112,116,122,126,131],[10,11,15],"alert",{"type":12,"icon":13,"title":14},"info","tabler:robot","AI 迁移提示",[16,17,18],"p",{},"本文由 AI 协助从旧站迁移，尚未完成逐篇人工审校；内容如有疏漏，将在复核后修订。",[20,21,22,25],"blockquote",{},[16,23,24],{},"尚未安装 PostgreSQL？参见",[26,27],"link-card",{"link":28,"title":29},"\u002Fposts\u002Fpostgresql-install","PostgreSQL 安装指南",[31,32,34],"h2",{"id":33},"一配置远程访问","一、配置远程访问",[36,37,39],"h3",{"id":38},"_1-修改认证方式","1. 修改认证方式",[10,41,43],{"type":42},"warning",[16,44,45,46,50,51,54,55,58],{},"以下操作会降低数据库安全性。",[47,48,49],"code",{"code":49},"trust"," 认证允许本地用户免密连接为任意数据库用户，仅建议在开发\u002F测试环境使用。生产环境应保持 ",[47,52,53],{"code":53},"peer"," 或使用 ",[47,56,57],{"code":57},"scram-sha-256","。",[60,61,67],"pre",{"className":62,"code":64,"language":65,"meta":66},[63],"language-bash","# 将 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","bash","",[47,68,64],{"__ignoreMap":66},[36,70,72],{"id":71},"_2-允许任意-ip-连接","2. 允许任意 IP 连接",[16,74,75,76,79],{},"编辑 ",[47,77,78],{"code":78},"\u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpg_hba.conf","，添加：",[10,81,82],{"type":42},[16,83,84,87],{},[47,85,86],{"code":86},"0.0.0.0\u002F0"," 表示允许所有 IP 连接，生产环境应限制为特定 IP 段，并确保使用强密码。",[60,89,94],{"className":90,"code":92,"language":93},[91],"language-text","# 允许所有 IP 使用密码连接\nhost   all   all   0.0.0.0\u002F0   md5\n","text",[47,95,92],{"__ignoreMap":66},[36,97,99],{"id":98},"_3-监听所有网络接口","3. 监听所有网络接口",[16,101,75,102,105],{},[47,103,104],{"code":104},"\u002Fetc\u002Fpostgresql\u002F14\u002Fmain\u002Fpostgresql.conf","，取消注释并修改：",[60,107,110],{"className":108,"code":109,"language":93},[91],"listen_addresses = '*'\n",[47,111,109],{"__ignoreMap":66},[36,113,115],{"id":114},"_4-重启服务","4. 重启服务",[60,117,120],{"className":118,"code":119,"language":65,"meta":66},[63],"sudo systemctl restart postgresql\nsudo systemctl enable postgresql\n",[47,121,119],{"__ignoreMap":66},[31,123,125],{"id":124},"二数据目录迁移","二、数据目录迁移",[10,127,128],{"type":42},[16,129,130],{},"数据目录迁移操作风险较高，权限设置不正确或拷贝不完整会导致 PostgreSQL 无法启动。操作前务必停止服务并做好备份。",[60,132,135],{"className":133,"code":134,"language":65,"meta":66},[63],"# 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",[47,136,134],{"__ignoreMap":66},{"title":66,"searchDepth":138,"depth":138,"links":139},4,[140,148],{"id":33,"depth":141,"text":34,"children":142},2,[143,145,146,147],{"id":38,"depth":144,"text":39},3,{"id":71,"depth":144,"text":72},{"id":98,"depth":144,"text":99},{"id":114,"depth":144,"text":115},{"id":124,"depth":141,"text":125},[150],"技术","2026-01-25 00:00:00","介绍 PostgreSQL 的远程访问配置（pg_hba.conf \u002F postgresql.conf）以及将数据目录从默认路径迁移到自定义路径的操作步骤。",false,"md",null,{"slots":157},{},true,"\u002Fposts\u002Fpostgresql-config",{"text":161,"minutes":162,"time":163,"words":164},"2 min read",1.86,111600,372,{"title":5,"description":152},{"loc":159},"posts\u002Fposts\u002Fpostgresql-config",[169,170],"PostgreSQL","数据库","tech","b_xGH8LLTe7M5NniRjhXUvdH7LcHHiqPGV-QsckrZT0",[174,179],{"title":175,"path":176,"stem":177,"date":178,"type":171,"children":-1},"docker-compose配置文件详解","\u002Fposts\u002Fdocker-compose-yml","posts\u002Fposts\u002Fdocker-compose-yml","2026-01-25",{"title":180,"path":181,"stem":182,"date":183,"type":171,"children":-1},"PostgreSQL 外部表（FDW）跨库查询配置","\u002Fposts\u002Fpostgresql-fdw","posts\u002Fposts\u002Fpostgresql-fdw","2026-01-26 00:00:00",1788712224824]