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

相关新闻

洛谷 P1014 [NOIP 1999 普及组] Cantor 表

洛谷 P1014 [NOIP 1999 普及组] Cantor 表

本文由Jzwalliser原创,发布在CSDN平台上,遵循CC 4.0 BY-NC-SA协议。 因此,若需转载/引用本文,请注明作者并附原文链接,不得用于商业用途。 违者必究,谢谢配合。 个人主页:blog.csdn.net/jzwalli…

2026/11/4 4:11:07 阅读更多 →
《AutoFocus: Efficient Multi-Scale Inference》论文笔记

《AutoFocus: Efficient Multi-Scale Inference》论文笔记

《AutoFocus: Efficient Multi-Scale Inference》论文笔记 1. Motivation 这是一篇讨论如何 “from coarse to fine” 进行目标检测的论文。进行多尺度目标检测一般都会用到金字塔网络,有的是利用特征金字塔如FPN(Feature Pyramid Network)在…

2026/11/5 9:03:24 阅读更多 →
HunyuanImage-2.1创意案例大全:8个场景教你用AI生成商业级视觉内容

HunyuanImage-2.1创意案例大全:8个场景教你用AI生成商业级视觉内容

HunyuanImage-2.1创意案例大全:8个场景教你用AI生成商业级视觉内容 【免费下载链接】HunyuanImage-2.1 HunyuanImage-2.1: An Efficient Diffusion Model for High-Resolution (2K) Text-to-Image Generation​ 项目地址: https://gitcode.com/gh_mirrors/hu/Huny…

2026/11/5 8:39:03 阅读更多 →

最新新闻

Electron桌面应用开发实战:从环境搭建到打包发布全流程

Electron桌面应用开发实战:从环境搭建到打包发布全流程

做前端这些年,我大部分时间都在跟浏览器标签页打交道,直到第一次把桌面应用跑起来,才发现原来自己一直被限制在地址栏和刷新按钮围起来的框框里。Electron这个词,最开始是在某段时间频繁出现在同事的讨论里,一个用HTML…

2026/11/5 12:28:00 阅读更多 →
滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

滑盖智能锁T109深度解析:结构原理、安装调试与故障排查

简介:亚太天能T109滑盖智能锁产品说明书是一份PDF格式的官方技术文档,主要面向智能锁安装人员、终端用户及售后维护者,帮助快速掌握产品安装、配置与常见故障处理。全包仅1个PDF文件,大小4.37MB,内容结构清晰&#xff…

2026/11/5 1:08:50 阅读更多 →
PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

PMSM弱磁控制原理与Simulink建模:高速AGV驱动系统设计指南

上个月我在调试一台高速AGV的驱动系统,整车要求空载跑到8m/s,电机转速已经拉到额定点,驱动器的输出电压也顶到了母线电压极限附近。再往上踩速度指令,转速纹丝不动,三相电流反而开始震荡。那会儿我的第一反应就是&…

2026/11/5 12:28:01 阅读更多 →
C++核心知识点:从内存管理到STL的实战指南

C++核心知识点:从内存管理到STL的实战指南

C 这门语言我前前后后折腾了十几年,用它写过嵌入式驱动、做过游戏服务端,也搭过基础组件库。每次看到新人一头扎进"C 核心知识点"这个搜索词,我其实挺感慨的——这门语言的难点从来不是语法多,而是知识点之间的耦合太深…

2026/11/5 12:28:02 阅读更多 →
T109滑盖智能锁怎么装?安装调试与故障排查全攻略

T109滑盖智能锁怎么装?安装调试与故障排查全攻略

简介:亚太天能T109滑盖智能锁产品说明书以PDF格式发布,适合智能锁用户、安装人员与售后维修人员阅读。内容围绕T109系列指纹密码刷卡锁展开,先介绍前面板、后面板、锁体、机械钥匙、电池盒盖等部件名称与功能,再给出完整安装步骤&…

2026/11/5 0:54:28 阅读更多 →
LLM知识降熵:从信息垄断到理解权下放

LLM知识降熵:从信息垄断到理解权下放

你有没有过这种经历:翻开源码注释、论文摘要或者一份专业报告,每个字都认识,连在一起却像天书。想找人聊聊,身边要么没人懂,要么懂的懒得跟你解释。后来我试着把同一段内容丢给一个大语言模型,几秒钟之后&a…

2026/11/5 19:38:59 阅读更多 →

日新闻

周新闻

月新闻