超详细- linux 离线安装 postgresql12 和 postgis3.1 插件

超详细- linux 离线安装 postgresql12 和 postgis3.1 插件


一、安装 postgresql

1、解压 linux 版本 postgresql 压缩包

# 解压
tar -zxvf postgresql-12.4.tar.gz

# 进入解压后的文件
cd postgresql-12.4

2、configure安装环境检测

# 加 --prefix参数,用于指定软件安装的位置
./configure --prefix=/usr/local/postgresql  

如果提示 confiqure: error: readline library not found​,则需要手动下载 readline 相关的压缩包进行安装。

centos-7-os-x86_64-Packages安装包下载_开源镜像站-阿里云 这个网站下可以下载你需要的 rpm 依赖包。

# 安装相关的依赖

rpm -ivh ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm --nodeps --force
rpm -ivh readline-devel-6.2-11.el7.x86_64.rpm  --nodeps --force

如果编译过程中又报 error:zlib library not found​ 错误 ,继续找到包 zlib-devel-1.2.7-18.el7.x86_64.rpm ,拷贝安装。

rpm -ivh zlib-devel-1.2.7-18.el7.x86_64.rpm --nodeps --force

3、编译安装

# 会自动生成一个/usr/local/postgresql目录
./configure --prefix=/usr/local/postgresql
make & make install

4、创建 postgres 用户与数据库 data 目录

# 创建 postgres 组与 postgres 用户
groupadd postgres
useradd -g postgres -m postgres

# 更改用户密码
passwd postgres

# /usr/local/postgresql 目录下创建 data 文件夹,并将其权限授予postgrs
mkdir -p /usr/local/postgresql/data
chown postgres:postgres /usr/local/postgresql/data/

5、配置环境变量

vim /etc/profile

export LD_LBRARY_PATH=/usr/local/postgresql/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/postgresql/bin:$PATH
export MANPATH=/usr/local/postgresql/man:$MANPATH

# 生效环境变量
source /etc/profile

6、初始化数据库

# 创建 log 文件
touch /usr/local/postgresql/logfile

# 授权
chown postgres:postgres /usr/local/postgresql/logfile

# 切换用户
su - postgres

# 初始化数据库
initdb -D /usr/local/postgresql/data

# 启动服务
pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/logfile start

7、修改配置文件

配置文件在 /usr/local/postgresql/data​ 目录下

# 允许远程连接
vi /usr/local/postgresql/data/pg_hba.conf

#在文件的最下方加上下面的这句话(出于安全考虑,不建议这样配置)
host    all         all         0.0.0.0/0             trust

# 设置监听整个网络
vi /usr/local/postgresql/data/postgresql.conf

#修改为如下:
listen_addresses = '*' 
port = 5432
max_connections = 200   # 最大连接数

提升 pgsql 数据库性能

# 针对 32GB 内存的专用 PostgreSQL 服务器

# 核心内存配置
shared_buffers = 8GB    #数据缓存(25% 总内存)
work_mem = 32MB    #排序/哈希操作内存
maintenance_work_mem = 2GB    #VACUUM/REINDEX 等维护操作内存(总内存 6%)
effective_cache_size = 24GB    #优化器估算的 OS 文件缓存(总内存 75%)
wal_buffers = 16MB    #WAL 日志缓冲区(推荐值,自动上限)

# 写入优化
max_wal_size = 4GB    # WAL 最大尺寸 (shared_buffers 的 50%)
min_wal_size = 2GB    # WAL 最小尺寸
checkpoint_completion_target = 0.9    # 平滑写入负载
wal_compression = on    # 压缩 WAL(减少 I/O,CPU 换空间)
commit_delay = 10    # 组提交延迟(微秒级,提升高并发写入)

# 重启服务
 pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/logfile restart

# 查看端口是否启用
netstat -anp | grep 5432

8、设置开机自启

# 拷贝源安装包下postgres-12.4/contrib/start-scripts/linux至etc/init.d中,并命名为postgresql
cp /data/postgresql-12.4/contrib/start-scripts/linux /etc/init.d/postgresql

# 更改 postgresql 文件中的目录
vim /etc/init.d/postgresql

prefix=/usr/local/postgresql
PGDATA="/usr/local/postgresql/data"

# 授权
chmod +x /etc/init.d/postgresql

# 加入开机启动

chkconfig --add postgresql
chkconfig postgresql on
systemctl daemon-reload
systemctl start postgresql.service
systemctl status postgresql.service
systemctl enable postgresql.service

二、离线安装 postgis 插件

1、安装 proj4

tar xf proj-4.8.0.tar.gz
cd proj-4.8.0
./configure --prefix=/usr/local/postgresql/plugin/proj
make && make install

# 配置和加载动态链接库
vim /etc/ld.so.conf.d/proj-4.8.0.conf

/usr/local/postgresql/plugin/proj/lib

# 更新库缓存
ldconfig

2、安装 geos

bzip2 -d geos-3.6.5.tar.bz2
tar xf geos-3.6.5.tar
./configure --prefix=/usr/local/postgresql/plugin/geos
make && make install

# 配置和加载动态链接库
vim /etc/ld.so.conf.d/geos-3.6.5.conf

/usr/local/postgresql/plugin/geos/lib

# 更新库缓存
ldconfig

3、安装 gdal

① 安装 gdal 所需要的依赖 json-c

rpm -ivh json-c-0.11-4.el7_0.x86_64.rpm --nodeps --force
rpm -ivh json-c-devel-0.11-4.el7_0.x86_64.rpm --nodeps --force

② 安装 gdal

tar xf gdal-2.0.1.tar.gz
cd gdal-2.0.1
./configure --prefix=/usr/local/postgresql/plugin/gdal  --with-json-c=/usr  --with-zlib=/usr
make && make install

# 配置和加载动态链接库
vim /etc/ld.so.conf.d/gdal-2.0.1.conf

/usr/local/postgresql/plugin/gdal/lib

# 更新库缓存
ldconfig

4、安装 PostGIS

tar xf postgis-3.1.0.tar.gz
cd postgis-3.1.0
./configure --prefix=/usr/local/postgresql/plugin/postgis --with-pgconfig=/usr/local/postgresql/bin/pg_config --with-geosconfig=/usr/local/postgresql/plugin/geos/bin/geos-config --with-gdalconfig=/usr/local/postgresql/plugin/gdal/bin/gdal-config --with-projdir=/usr/local/postgresql/plugin/proj --without-protobuf

如果有configure: error: could not find xml2-config from libxml2 within the current path. You may need to try re-running configure with a --with-xml2config parameter​这个报错

解决办法:下载这个包并安装

# 查看是否存在xml2-config文件,结果不存在
find / -name "xml2-config"

# 查看是否已经安装了libxml2和libxml2-devel
rpm -qa |grep  libxml2

# 下载libxml2-devel压缩包并安装
rpm -ivh libxml2-devel-2.9.1-6.el7.5.x86_64.rpm --nodeps --force

5、检查 PostGIS 是否安装成功

# 启用 postgis_tiger_geocoder 需要先安装 fuzzystrmatch
# 在 postgresql 的源码安装包内的contrib目录下,进入 fuzzystrmatch 的目录
make && make install

# 确保 plugin 目录所有权正确(当前是 root)
chown -R postgres:postgres /usr/local/postgresql/plugin/

# 开启 postgis 插件

CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION postgis_tiger_geocoder;
CREATE EXTENSION address_standardizer; 
posted @ 2025-08-19 15:50  糜9  阅读(375)  评论(1)    收藏  举报