恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

ubuntu 源码安装postgresql16.0

  • 首页
  • 资讯中心
  • /
  • ubuntu 源码安装postgresql16.0

相关资讯

5分钟搭建专业AI姿势设计工作站:3D OpenPose Editor完全指南 2026/8/11 16:53:52
TreeSheets:如何用分层电子表格彻底改变你的数据组织方式? 2026/8/11 16:53:52
D3SL-P安全总线多功能安全门锁,车间防护门别只装一个简易拉手,还在用普通拉手当做设备防护门锁? 2026/8/11 16:53:52

最新资讯

如何在 Windows、macOS 和 Linux 上安装 scikit-learn (sklearn)
【单片机毕业设计】光传感联动 LED 的 STM32 智能快递柜硬件控制系统实现 基于 STM32 的可自定义光照阈值快递存取终端设计(017102)
计算机单片机毕设实战-基于 STM32 的阈值可调光感快递柜短信取件系统设计 STM32 驱动的带 OLED 显示智能快递存储设备研发(017102)
单片机毕业设计-基于 STM32 的阈值可调光感快递柜短信取件系统设计 STM32 驱动的带 OLED 显示智能快递存储设备研发(017102)
无用之用,方为大用
深入理解NSEtcHosts源码:EtcHostsURLProtocol类解析

今日推荐

《人工智能导论:深度学习大模型基础》全套PPT课件2026
9.5 技术债务的重构:何时该动一次大手术
如何用Video2X实现专业级视频画质提升:AI视频增强完整指南

本周热门

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁
如何快速生成中国车牌图片:Python开源工具完整指南
当 LLM 遇见大文档:主流开源项目如何处理上下文超限

本月精选

如何用DamaiHelper实现演唱会门票的智能自动化抢购:完整技术解决方案指南
第4篇:59 倍性能差距的索引瓶颈定位——一次教科书级的全表扫描调优
终极歌词批量下载神器:5分钟解决离线音乐库歌词同步难题

ubuntu 源码安装postgresql16.0

发布时间:2026/8/11 16:58:52
ubuntu 源码安装postgresql16.0 使用 root 账户进行安装安装路径/opt/pgsql16 手动创建数据存储路径/data/pgsql16/data 手动创建数据库配置文件 /data/pgsql16/data/postgresql.conf 会自动生成自动安装脚本参考最后 install_postgresql.sh(我是在ubuntu 22.04 上执行的)开始安装刷新本地包索引、安装相关依赖apt-getupdate-yapt-getupgrade-yapt-getinstallmake-yapt-getinstallbuild-essential-yapt-getinstalllibreadline-dev-yapt-getinstallzlib1g zlib1g-dev-yapt-getinstallbison-yapt-getinstallflex-yapt-getinstalllibpq-dev-yapt-getinstalllibicu-dev-yapt-getinstalllibcommon-sense-perl-yapt-getinstalllibjson-perl-yapt-getinstalllibjson-xs-perl-yapt-getinstalllibpq5-yapt-getinstalllibtypes-serialiser-perl-yapt-getinstallsysstat-y下载源码并解压(安装完之后可以删除的以防后期混淆)cd/wgethttps://ftp.postgresql.org/pub/source/v16.0/postgresql-16.0.tar.gztar-zxvfpostgresql-16.0.tar.gz# 进入解压目录cdpostgresql-16.0创建安装路径和数据存储路径(配置文件也在这个里面)mkdir-p/opt/pgsql16 /data/pgsql16/data配置编译参数并进行编译验证是否安装成功–prefix 指定安装路径–without-icu 禁用ICU 支持./configure--prefix/opt/pgsql16 --without-icu#编译 (持续时间较长)makemakeinstall#验证是否安装成功 (有配置内容则成功了)./opt/pgsql16/bin/pg_config#如果提示这个没见不存在就cd /opt/pgsql16/bin 然后执行 ./pg_config创建postgres用户与用户组 修改文件所有者groupaddpostgresuseradd-gpostgres postgrespasswdpostgres# 后面输入密码这里不是数据库密码chownpostgres:postgres /data/pgsql16/datamkdir/home/postgreschmod755/home/postgreschownpostgres:postgres /home/postgrescp-a/etc/skel/. /home/postgresvim/etc/passwd# 最后一行为postgres:x:xxxx:xxxx::/home/postgres:/bin/sh 将sh 修改成bash#postgres:x:xxxx:xxxx::/home/postgres:/bin/bash配置环境变量vim /etc/profile 最后添加如下exportPGHOME/opt/pgsql16exportPGDATA/data/pgsql16/dataexportPATH$PATH:$PGHOME/bin重新加载系统环境变量source/etc/profile切换用户 初始化数据库su- postgrescd/data/pgsql16/data# initdb 直接输入回车就行了initdb-D/data/pgsql16/data出现下面内容表示成功Success. You can now start the database server using:pg_ctl -D /data/pgsql16/data -l logfile start设置开机启动 切换到 root用户suroot编辑 vim /postgresql-16.0/contrib/start-scripts/linux 文件修改如下文件在解压后的源码包里。# 安装路径prefix/opt/pgsql16# 数据存储路径PGDATA/data/pgsql16/data并且修改文件头部 为 #! /bin/bash 并且添加下列代码不要取消注释BEGIN 部分也得加上不然启动会报错#! /bin/bash### BEGIN INIT INFO# Provides: hutu# Required-Start:# Required-Stop:# Default-Start: 2 3 4 5# Default-Stop: 0 1 6# Short-Description: Start hutu daemon at boot time# Description: Start hutu daemon at boot time### END INIT INFO修改成可执行chmodax /postgresql-16.0/contrib/start-scripts/linux复制到 /etc/init.d/、/usr/local/bin/ 文件夹下去,并且改成postgresqlcp/postgresql-16.0/contrib/start-scripts/linux /etc/init.d/postgresqlcp/postgresql-16.0/contrib/start-scripts/linux /usr/local/bin/postgresqlchmodx /usr/local/bin/postgresql创建system服务vim /etc/systemd/system/postgresql.service[Unit]DescriptionPostgreSQL RDBMSAfternetwork.target[Service]TypeforkingUserpostgresGrouppostgresExecStart/opt/pgsql16//bin/pg_ctl start-D/data/pgsql16/data/-s-o-p 5432ExecStop/opt/pgsql16//bin/pg_ctl stop-D/data/pgsql16/data/-s-mfastExecReload/opt/pgsql16//bin/pg_ctl reload-D/data/pgsql16/data/-s[Install]WantedBymulti-user.target刷新系统服务设置开机启动启动服务systemctl daemon-reload systemctlenablepostgresql.service systemctl start postgresql systemctl status postgresql修改配置文件 /data/pgsql16/data/postgresql.conf允许远程访问listen_addresses 这个值改成 * 其他配置配置另行百度修改配置文件 /data/pgsql16/data/pg_hba.conf 最后一行添加如下hostall all0.0.0.0/0 md5重启服务systemctl restart postgresql修改数据库的postgres账户密码,放开防火墙su- postgres# 登录数据库(上方 /data/pgsql16/data/pg_hba.conf# 文件默认有 host all all 127.0.0.1/32 trust 这个 其中 trust 表示不需要密码登录)psql# 修改密码alter user postgres with password你的密码;exitsuroot#放开防火墙ufw allow5432删除安装包和源码以防后期混淆rm-rf/postgresql-16.0.tar.gzrm-rf/postgresql-16.0脚本安装(root用户)随便在哪个位置 新建 install_postgresql.sh 粘贴以下脚本 并设置可以执行权限 chmod x install_postgresql.sh#!/bin/bash# PostgreSQL 16 源码安装脚本 (基于 Ubuntu)# 参考: https://blog.csdn.net/qq_36491545/article/details/140488108# 注意此脚本需使用 root 用户执行 (sudo)set-e# 脚本在遇到错误时立即退出# --- 配置变量 (可按需修改) ---PG_VERSION16.0PG_HOME/opt/pgsql16PG_DATA/data/pgsql16/dataPG_USERpostgresPG_PASSWORDyour_strong_password# 请修改为强密码PG_PORT5432# --- 函数: 打印带颜色的提示信息 ---print_info(){echo-e\n\033[32m[INFO]\033[0m$1}print_error(){echo-e\n\033[31m[ERROR]\033[0m$12}# --- 1. 更新系统并安装依赖 ---print_info更新系统包列表并安装依赖...apt-getupdate-yapt-getupgrade-yapt-getinstall-ymakebuild-essential libreadline-dev zlib1g zlib1g-dev\bison flex libpq-dev libicu-dev libcommon-sense-perl libjson-perl\libjson-xs-perl libpq5 libtypes-serialiser-perl sysstatwget# --- 2. 下载并解压源码 ---print_info下载 PostgreSQL${PG_VERSION}源码...cd/if[-fpostgresql-${PG_VERSION}.tar.gz];thenrm-fpostgresql-${PG_VERSION}.tar.gzfiwgethttps://ftp.postgresql.org/pub/source/v${PG_VERSION}/postgresql-${PG_VERSION}.tar.gzprint_info解压源码...tar-zxvfpostgresql-${PG_VERSION}.tar.gzcdpostgresql-${PG_VERSION}# --- 3. 创建目录 ---print_info创建安装目录和数据目录...mkdir-p$PG_HOME$PG_DATA# --- 4. 编译与安装 ---print_info配置编译选项 (禁用ICU支持)..../configure--prefix$PG_HOME--without-icu print_info开始编译 (此过程较长, 请耐心等待)...make-j$(nproc)# 使用所有CPU核心加速编译makeinstallprint_info编译安装完成.# 验证安装if[-x$PG_HOME/bin/pg_config];thenprint_info安装验证成功:$PG_HOME/bin/pg_config--versionelseprint_error安装验证失败, pg_config 未找到.exit1fi# --- 5. 创建 postgres 用户 ---print_info创建系统用户 ${PG_USER}...ifid$PG_USER/dev/null;thenprint_info用户 ${PG_USER} 已存在跳过创建。elsegroupadd$PG_USERuseradd-g$PG_USER$PG_USERecho${PG_USER}:${PG_PASSWORD}|chpasswdmkdir-p/home/${PG_USER}chmod755/home/${PG_USER}chown${PG_USER}:${PG_USER}/home/${PG_USER}cp-a/etc/skel/. /home/${PG_USER}# 修改默认 shell 为 bashusermod-s/bin/bash$PG_USERfi# 修改数据目录所有者chown-R${PG_USER}:${PG_USER}$PG_DATAchown-R${PG_USER}:${PG_USER}$PG_HOME# --- 6. 配置环境变量 ---print_info配置 PostgreSQL 环境变量...grep-qPGHOME/etc/profile||cat/etc/profileEOF export PGHOME$PG_HOMEexport PGDATA$PG_DATAexport PATH\$PATH:\$PGHOME/bin EOFsource/etc/profile# --- 7. 初始化数据库 ---print_info切换至${PG_USER}用户初始化数据库...su-$PG_USER-cinitdb -D$PG_DATA# --- 8. 配置 systemd 服务 (替代原博客的 init.d) ---print_info配置 systemd 服务...cat/etc/systemd/system/postgresql.serviceEOF [Unit] DescriptionPostgreSQL RDBMS Afternetwork.target [Service] Typeforking User${PG_USER}Group${PG_USER}ExecStart${PG_HOME}/bin/pg_ctl start -D${PG_DATA}-s -o -p${PG_PORT} ExecStop${PG_HOME}/bin/pg_ctl stop -D${PG_DATA}-s -m fast ExecReload${PG_HOME}/bin/pg_ctl reload -D${PG_DATA}-s ExecStatus${PG_HOME}/bin/pg_ctl status -D${PG_DATA}Restartno [Install] WantedBymulti-user.target EOF# 重新加载 systemd 并启用服务systemctl daemon-reload systemctlenablepostgresql.service print_infoPostgreSQL systemd 服务已配置并启用.# --- 9. 启动服务并检查状态 ---print_info启动 PostgreSQL 服务...systemctl start postgresqlsleep3systemctl status postgresql --no-pager# --- 10. 配置远程访问 (可选) ---print_info配置远程访问权限...sed-is/#listen_addresses localhost/listen_addresses */g$PG_DATA/postgresql.confechohost all all 0.0.0.0/0 md5$PG_DATA/pg_hba.conf# 重启服务使配置生效systemctl restart postgresql# --- 11. 修改数据库管理员密码 ---print_info修改 PostgreSQL 数据库 postgres 用户密码...su-$PG_USER-cpsql -c\ALTER USER postgres WITH PASSWORD ${PG_PASSWORD};\# --- 12. 设置防火墙 (如果 UFW 启用) ---ifcommand-vufw/dev/nullufw status|grep-qactive;thenprint_info检测到 UFW 防火墙已启用开放端口${PG_PORT}...ufw allow${PG_PORT}/tcpfi# --- 13. 清理安装文件 ---print_info清理下载的源码包和解压目录...cd/rm-rfpostgresql-${PG_VERSION}.tar.gzpostgresql-${PG_VERSION}# --- 完成 ---print_infoPostgreSQL${PG_VERSION}源码安装成功!print_info安装路径:${PG_HOME}print_info数据路径:${PG_DATA}print_info管理员用户:${PG_USER}, 密码:${PG_PASSWORD}print_info服务管理命令: systemctl {start|stop|restart|status} postgresql

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号