PostgreSQL安装手册

PostgreSQL在线安装

PostgreSQL版本:11.15

PostGIS版本:3.1

安装PostgreSQL###

1、添加yum源

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

2、安装postgresql 11

yum install -y postgresql11-server

3、初始化设置

/usr/pgsql-11/bin/postgresql-11-setup initdb

4、启动postgresql并设置开机自启

systemctl start postgresql-11
systemctl enable postgresql-11

5、修改progres用户(系统用户)密码

passwd postgres
回车输入两次新密码

6、修改数据库用户progres(postgresql超级管理员)密码

su - postgres                                                   //切换到postgres用户 
psql                                                            //登录postgresql 
postgres=# ALTER ROLE postgres WITH PASSWORD 'postgrespass';    //postgrespass为密码

7、修改配置

# vim /var/lib/pgsql/11/data/postgresql.conf
......
listen_addresses = '*'                      #监听地址,"*"号代表侦听本机所有ip
......


将认证方式修改为密码认证
# vim /var/lib/pgsql/11/data/pg_hba.conf
......
# "local" is for Unix domain socket connections only
#local   all all peer
local   all all md5
# IPv4 local connections:
#hostall all 127.0.0.1/32scram-sha-256
hostall all 127.0.0.1/32trust               #本机不需要密码验证
hostall all 0.0.0.0/0   md5                 #其他ip需要密码验证
# IPv6 local connections:
#hostall all ::1/128 scram-sha-256
hostall all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication all peer
#hostreplication all 127.0.0.1/32scram-sha-256
#hostreplication all ::1/128 scram-sha-256
local   replication all md5
hostreplication all 127.0.0.1/32trust       #本机不需要密码验证
hostreplication all ::1/128 md5
......

重启postgresql服务生效

systemctl restart postgresql-11

8、防火墙放行5432端口

firewall-cmd --add-port=5432/tcp
firewall-cmd --add-port=5432/tcp --permanent

安装PostGIS插件

1. 添加epel源

    yum -y install epel-release.noarch

2. 安装对应版本的插件及其依赖

    yum -y install postgis31_11 postgis31_11-client
    yum -y install ogr_fdw11
    yum -y install pgrouting_11   

此处的11为 postgresql 版本号

postgresql和postgis版本对应关系:https://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS

PostgreSQL离线安装

说明:已打包离线yum源,已上传至附件,附件名为pgsql11_packages_new.tar.gz

PostgreSQL版本:11.15

PostGIS版本:3.1

配置本地yum源###

1、创建存放目录

mkdir /repo

2、上传打包好的离线yum源,并解压至/repo目录下

tar -zxvf pgsql11_packages.tar.gz -C /repo/

3、配置本地yum源

# mkdir /etc/yum.repos.d/bak
# mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/bak/

# vim /etc/yum.repos.d/local.repo//文件中添加以下内容
[local]
name=local
baseurl=file:///repo/pgsql_packages/
gpgcheck=0
enabled=1

验证yum源是否可用

# yum clean all
# yum repolist
Loaded plugins: fastestmirror
Determining fastest mirrors
local | 2.9 kB  00:00:00 
local/primary_db  |  96 kB  00:00:00 
repo id   repo namestatus
local local130
repolist: 130

如果repolist的值为0,则yum源存在问题

安装PostgreSQL###

1、安装postgresql 11

yum install -y postgresql11-server

2、初始化设置

/usr/pgsql-11/bin/postgresql-11-setup initdb

3、启动postgresql并设置开机自启

systemctl start postgresql-11
systemctl enable postgresql-11

4、修改progres用户(系统用户)密码

passwd postgres
回车输入两次新密码

5、修改数据库用户progres(postgresql超级管理员)密码

su - postgres//切换到postgres用户
psql//登录postgresql
postgres=# ALTER ROLE postgres WITH PASSWORD 'postgrespass';//postgrespass为密码

6、修改配置

# vim /var/lib/pgsql/11/data/postgresql.conf
......
listen_addresses = '*'//监听地址,"*"号代表侦听本机所有ip
......


将认证方式修改为密码认证
# vim /var/lib/pgsql/11/data/pg_hba.conf
......
# "local" is for Unix domain socket connections only
#local   all all peer
local   all all md5
# IPv4 local connections:
#hostall all 127.0.0.1/32scram-sha-256
hostall all 127.0.0.1/32trust#本机不需要密码验证
hostall all 0.0.0.0/0   md5     #其他ip需要密码验证
# IPv6 local connections:
#hostall all ::1/128 scram-sha-256
hostall all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication all peer
#hostreplication all 127.0.0.1/32scram-sha-256
#hostreplication all ::1/128 scram-sha-256
local   replication all md5
hostreplication all 127.0.0.1/32trust#本机不需要密码验证
hostreplication all ::1/128 md5
......

重启postgresql服务生效

systemctl restart postgresql-11

7、防火墙放行5432端口

firewall-cmd --add-port=5432/tcp
firewall-cmd --add-port=5432/tcp --permanent

安装PostGIS插件###

安装对应版本的插件及其依赖

yum -y install postgis31_11 postgis31_11-client
yum -y install ogr_fdw_11
yum -y install pgrouting_11

此处的11为 postgresql 版本号

postgresql和postgis版本对应关系:https://trac.osgeo.org/postgis/wiki/UsersWikiPostgreSQLPostGIS

PostgreSQL编译安装##

说明:

所需的rpm包已打包离线yum源,上传至附件,附件名为pgsql_packages_source_rpm.tar.gz

需要编译安装的各源码包也已打包上传至附件,附件名为pgsql_install_source.tar.gz

PostgreSQL版本:11.15
PostGIS版本:3.1.2

安装编译环境###

添加epel源

# yum -y install epel-release

安装依赖包和gcc等编译工具

# yum -y install gcc gcc-c++ wget llvm5.0 llvm5.0-devel clang libicu-devel perl-ExtUtils-Embed readline readline-devel zlib zlib-devel openssl openssl-devel pam-devel libxml2-devel libxslt-devel openldap-devel systemd-devel tcl-devel python-devel bzip2 gmp-devel mpfr-devel

编译安装postgresql###

创建用户并设置密码

# useradd postgres
# passwd postgres

创建安装目录

# mkdir /data/pgsql11 -p

上传源码包

源码包下载地址:https://ftp.postgresql.org/pub/source/

解压源码包

# tar -zxvf postgresql-11.15.tar.gz

初始化

# cd postgresql-11.15/

# ./configure --prefix=/data/pgsql11 --enable-nls --with-python --with-tcl --with-gssapi --with-icu --with-openssl --with-pam --with-ldap --with-systemd --with-libxml --with-libxslt --with-perl --enable-thread-safety --enable-debug

编译 && 安装

# make
# make install

安装contrib目录下的一些工具

# cd contrib
# make
# make install

编辑环境变量

# vim /home/postgres1/.bash_profile添加下列内容
PGHOME=/data/pgsql11
PGDATA=/data/pgdata
PATH=$PGHOME/bin:$PATH
export LD_LIBRARY_PATH=$PGHOME/lib
export PGDATA PATH PGHOME

创建数据目录

# mkdir /data/pgdata -p

修改所属人所属组

# chown postgres:postgres -R /data/pgsql11/
# chown postgres:postgres -R /data/pgdata/

切换到postgres用户

# su - postgres

初始化postgresql

$ /data/pgsql11/bin/initdb -D /data/pgdata/

创建日志存放目录

$ mkdir /data/pgdata/logs

启动postgresql

$ /data/pgsql11/bin/pg_ctl -D /data/pgdata/ -l /data/pgdata/logs/logfile start

登录postgresql

psql//登录postgresql
postgres=# ALTER ROLE postgres WITH PASSWORD 'password';//修改postgres用户密码

修改配置

$ vim /data/pgdata/postgresql.conf
......
listen_addresses = '*'//监听地址,"*"号代表监听本机所有ip
......
端口默认为5432
连接数默认为100

修改认证方式

$ vim /data/pgdata/pg_hba.conf
......
# "local" is for Unix domain socket connections only
local   all all md5
# IPv4 local connections:
hostall all 127.0.0.1/32trust//本机127.0.0.1允许免密登录
hostall all 0.0.0.0/0   md5
# IPv6 local connections:
hostall all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication all md5
hostreplication all 127.0.0.1/32trust
hostreplication all ::1/128 md5

重载postgresql配置

/data/pgsql11/bin/pg_ctl reload

配置为systemd管理

先停止postgresql服务

$ /data/pgsql11/bin/pg_ctl stop停止postgresql服务

切换回root用户 编辑.service文件

# vim /usr/lib/systemd/system/postgresql.service
[Unit]
Description=The PostgreSQL Database Server
After=syslog.target
After=network.target

[Service]
Type=forking
User=postgres
Group=postgres

ExecStart=/data/pgsql11/bin/pg_ctl start -D /data/pgdata -l /data/pgdata/logs/logfile
ExecStop=/data/pgsql11/bin/pg_ctl stop
ExecReload=/data/pgsql11/bin/pg_ctl reload -D /data/pgdata
TimeoutSec=300

[Install]
WantedBy=multi-user.target

启动postgresql

# systemctl start postgresql.service

编译安装PostGIS

安装的PostGIS为3.1,依赖的库有geos,proj,gdal,libxml,json-c,protobuf,如果要支持三维需安装sfcgal,如果要做路网分析需安装pgrouting(可单独安装)。

安装geos

# wget http://download.osgeo.org/geos/geos-3.9.1.tar.bz2
# tar -jxvf geos-3.9.1.tar.bz2
# cd geos-3.9.1

安装
# ./configure --prefix=/usr/local/geos-3.9.1
# make
# make install

# vim /etc/ld.so.conf添加
/usr/local/geos-3.9.1/lib

# ldconfig

安装proj

本次安装proj6.3.2,依赖较高版本的sqlite

先安装sqllite
# wget https://www.sqlite.org/2021/sqlite-autoconf-3340100.tar.gz
# tar -zxvf sqlite-autoconf-3340100.tar.gz
# cd sqlite-autoconf-3340100
# ./configure --prefix=/usr/local/sqlite
# make
# make install

替换系统旧的sqllite
# mv /usr/bin/sqlite3  /usr/bin/sqlite3_old
# ln -s /usr/local/sqlite/bin/sqlite3   /usr/bin/sqlite3
# sqlite3 --version
3.34.1 2021-01-20 14:10:07 10e20c0b43500cfb9bbc0eaa061c57514f715d87238f4d835880cd846b9ebd1f

将pkg_config暴露出去,避免proj找不到sqlite的问题
# export PKG_CONFIG_PATH=/usr/local/sqlite/lib/pkgconfig:$PKG_CONFIG_PATH

然后再安装proj6.3.2

# wget http://download.osgeo.org/proj/proj-6.3.2.tar.gz
# tar -zxvf proj-6.3.2.tar.gz
# cd proj-6.3.2

安装
# ./configure  --prefix=/usr/local/proj-6.3.2
# make
# make install

# vim /etc/ld.so.conf添加
/usr/local/proj-6.3.2/lib

# ldconfig

安装json-c

# wget https://github.com/json-c/json-c/archive/json-c-0.13.1-20180305.tar.gz
# tar -zxvf json-c-0.13.1-20180305.tar.gz
# cd json-c-json-c-0.13.1-20180305/

安装
# ./configure  --prefix=/usr/local/json-c-0.13.1
# make
# make install

# vim /etc/ld.so.conf添加
/usr/local/json-c-0.13.1/lib

# ldconfig

安装libxml

# tar -zxvf libxml2-2.9.7.tar.gz
# cd libxml2-2.9.7/

安装
# ./configure  --prefix=/usr/local/libxml2-2.9.7
# make
# make install

# vim /etc/ld.so.conf添加
/usr/local/libxml2-2.9.7/lib

# ldconfig

安装protobuf

# yum -y install autoconf
# yum -y install automake
# yum -y  install libtool libsysfs

# wget https://github.com/protocolbuffers/protobuf/archive/v3.10.1.tar.gz
或
# wget https://github.com/protocolbuffers/protobuf/releases/download/v3.10.1/protobuf-all-3.10.1.tar.gz

# protobuf-all-3.10.1.tar.gz
# cd protobuf-3.10.1/
# ./autogen.sh如果有configure则不需要这步

安装
# ./configure  --prefix=/usr/local/protobuf-3.10.1
# make
# make install

编辑环境变量
# vim /etc/profile添加以下内容
export PKG_CONFIG_PATH=/usr/local/protobuf-3.10.1/lib/pkgconfig
export PROTOBUF_HOME=/usr/local/protobuf-3.10.1
export PATH=$PROTOBUF_HOME/bin:$PATH

# source /etc/profile

验证
# protoc --version
libprotoc 3.10.1


# vim /etc/ld.so.conf添加
/usr/local/protobuf-3.10.1/lib

# ldconfig

安装protobuf-c

# wget https://github.com/protobuf-c/protobuf-c/releases/download/v1.3.2/protobuf-c-1.3.2.tar.gz
# tar -zxvf protobuf-c-1.3.2.tar.gz
# cd protobuf-c-1.3.2

安装
如果之前没设置PKG_CONFIG_PATH变量,需提前设置
export PKG_CONFIG_PATH=/usr/local/protobuf-3.10.1/lib/pkgconfig

# ./configure  --prefix=/usr/local/protobuf-c-1.3.2
# make
# make install

编辑环境变量
# vim /etc/profile添加
export PATH=:/usr/local/protobuf-c-1.3.2/bin:$PATH

# source /etc/profile


# vim /etc/ld.so.conf添加
/usr/local/protobuf-c-1.3.2/lib

# ldconfig

备注:如报错libprotobuf.so.21: cannot open shared object file: No such file or directory,执行以下操作再编译

# echo $LD_LIBRARY_PATH
# export LD_LIBRARY_PATH=/usr/local/protobuf-3.10.1/lib:\$LD_LIBRARY_PATH

LD _ LIBRARY_PATH是Linux环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径。

安装sfcgal(三维,可选)

sfcgal需要cmkae编译,需先安装下cmake

# wget https://github.com/Kitware/CMake/releases/download/v3.16.2/cmake-3.16.2.tar.gz
# tar -zxvf cmake-3.16.2.tar.gz
# cd cmake-3.16.2

安装
# ./configure --prefix=/usr/local/cmake-3.16.2
# make
# make install

编辑环境变量
# vim /etc/profile修改内容如下
export CMAKE_HOME=/usr/local/cmake-3.16.2
export PKG_CONFIG_PATH=/usr/local/protobuf-3.6.1/lib/pkgconfig
export PROTOBUF_HOME=/usr/local/protobuf-3.6.1
export PATH=$CMAKE_HOME/bin:$PROTOBUF_HOME/bin:$PATH

# source /etc/profile

sfcgal依赖boost,cgal,需要提前安装和编译,编译默认目录,避免编译sfcgal时各种找不到库的问题

# yum -y install boost-devel

# wget https://github.com/CGAL/cgal/archive/releases/CGAL-4.13.tar.gz
# tar -zxvf CGAL-4.13.tar.gz
# cd cgal-releases-CGAL-4.13
# mkdir build && cd build

安装cgal
# cmake ..
# make
# make install

编译安装sfcgal

# wget https://github.com/Oslandia/SFCGAL/archive/v1.3.7.tar.gz
# tar -zxvf v1.3.7.tar.gz
# cd SFCGAL-1.3.7

安装
# mkdir build && cd build
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/sfcgal-1.3.7 ..
# make
# make install

# vim /etc/ld.so.conf添加
/usr/local/sfcgal-1.3.7/lib64

# ldconfig

安装gdal

# wget https://download.osgeo.org/gdal/3.0.2/gdal-3.0.2.tar.gz
# tar -zxvf gdal-3.0.2.tar.gz
# cd gdal-3.0.2/

安装
# ./configure  --prefix=/usr/local/gdal-3.0.2 --with-pg=/data/pgsql11/bin/pg_config --with-proj=/usr/local/proj-6.3.2
# make
# make install

# vim /etc/ld.so.conf添加
/usr/local/gdal-3.0.2/lib

# ldconfig

安装PostGIS

源码下载地址:http://download.osgeo.org/postgis/source/

# tar -zxvf postgis-3.1.2.tar.gz
# cd postgis-3.1.2/

# ./configure --prefix=/usr/local/postgis31 --with-gdalconfig=/usr/local/gdal-3.0.2/bin/gdal-config --with-pgconfig=/data/pgsql11/bin/pg_config --with-geosconfig=/usr/local/geos-3.9.1/bin/geos-config --with-projdir=/usr/local/proj-6.3.2 --with-xml2config=/usr/local/libxml2-2.9.7/bin/xml2-config --with-jsondir=/usr/local/json-c-0.13.1 --with-protobufdir=/usr/local/protobuf-c-1.3.2 --with-sfcgal=/usr/local/sfcgal-1.3.7/bin/sfcgal-config

# make
# make install

修改权限

# chown postgres:postgres -R /data/pgdata/
# chown postgres:postgres -R /data/pgsql11/

重启postgresql

# systemctl stop postgresql.service
# systemctl start postgresql.service

你可能感兴趣的:(PostgreSQL安装手册)