云服务器安装

因为在服务器上安装有两个版本的PSQL,所以首先应该确定到底是哪个版本的PSQL在提供服务器,并将该版本的服务的命令设置为默认的命令,否则在下面安装过程中会产生错误。

[root@localhost ~]# netstat -ntlpa | grep post
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      741/postgres
tcp        0      0 127.0.0.1:5433          0.0.0.0:*               LISTEN      740/postgres
tcp        0      0 172.24.208.193:5432     172.24.208.192:56248    ESTABLISHED 24049/postgres: 10/
tcp        0      0 172.24.208.193:5432     172.24.208.192:54404    ESTABLISHED 11296/postgres: 10/
tcp        0      0 172.24.208.193:5432     172.24.208.192:54402    ESTABLISHED 11295/postgres: 10/
tcp        0      0 172.24.208.193:5432     172.24.208.192:58274    ESTABLISHED 2670/postgres: 10/m
tcp        0      0 172.24.208.193:5432     172.24.208.192:54406    ESTABLISHED 11297/postgres: 10/
tcp        0      0 172.24.208.193:5432     172.24.208.192:54400    ESTABLISHED 11294/postgres: 10/
tcp        0      0 172.24.208.193:5432     172.24.208.192:54410    ESTABLISHED 11299/postgres: 10/
#由此可以确定提供服务的是postgres10
[root@localhost ~]# ps -ef | grep post
postgres   741     1  0 Mar11 ?        00:16:33 /usr/lib/postgresql/10/bin/postgres -D /var/lib/postgresql/10/main -c config_file=/etc/postgresql/10/main/postgresql.conf
#由此命令查看postgres10的二进制命令所在目录为`/usr/lib/postgresql/10/bin/`
[root@localhost ~]# echo "export PATH=/usr/lib/postgresql/10/bin/:$PATH" >> /etc/profile
[root@localhost ~]# source /etc/profile
#将postgres10的命令设置为默认执行的二进制命令

1、为PostGresql安装rum插件

#安装依赖和postgres相关文件
$ git clone https://github.com/postgrespro/rum
$ cd rum
$ make USE_PGXS=1
$ make USE_PGXS=1 install
$ make USE_PGXS=1 installcheck
$ psql ladybird -c "CREATE EXTENSION rum;"

2、安装pg_jieba插件:

git clone https://github.com/jaiminpan/pg_jieba
cd pg_jieba
git submodule update --init --recursive
#此命令如果下载慢的话,可以直接执行`git clone git://github.com/yanyiwu/cppjieba.git`,然后在改名为libjieba
mkdir build
cd build
cmake ..
make
make install
$ psql ladybird -c "CREATE EXTENSION pg_jieba;"

在Docker中安装

使用docker部署postgres

[root@localhost ~]# docker pull postgres:10.13
[root@localhost ~]# mkdir /data/db/postgres1013
[root@localhost ~]# docker run -itd -e POSTGRES_USER=root -e POSTGRES_PASSWORD=111111 -p 54320:5432 -v /data/db/postgres1013:/var/lib/postgresql/data --restart=always --detach=true --name postgres_ver_1013 postgres:10.13 postgres

如果启用了防火墙则需要添加防火墙规则,命令如下:

[root@localhost ~]# docker inspect postgres_ver_1013 | grep "IPAddress" | tail -1
#查询该容器的IP地址,为172.17.0.12
[root@localhost ~]# ifconfig
#查询172.17.0网段所在的网卡名称,此处为docker0
[root@localhost ~]# vim /etc/sysconfig/iptables
-A POSTROUTING -s 172.17.0.12/32 -d 172.17.0.12/32 -p tcp -m tcp --dport 54320 -j MASQUERADE
#添加防火墙规则
[root@localhost ~]# systemctl restart iptables.service
#重新加载防火墙规则

替换容器软件源

root@511974524dcc:~# cp /etc/apt/sources.list{,.bak}
root@511974524dcc:~# > /etc/apt/sources.list
root@511974524dcc:~# cat >> /etc/apt/sources.list << EOF
deb http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch main non-free contrib
deb http://mirrors.aliyun.com/debian-security stretch/updates main
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main
deb http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib
EOF

docker容器中安装

在docker容器中的安装步骤与云服务器步骤基本一致,不过需要注意的是,在docker中是没有postgres开发库的,所以必须手动安装:

apt-get update
apt-get install -y --no-install-recommends ca-certificates gcc g++ libc6-dev cmake make git postgresql-server-dev-10 libpq-dev

1、为PostGresql安装rum插件

#安装依赖和postgres相关文件
$ git clone https://github.com/postgrespro/rum
$ cd rum
$ make USE_PGXS=1
$ make USE_PGXS=1 install
$ make USE_PGXS=1 installcheck
$ psql ladybird -c "CREATE EXTENSION rum;"

2、安装pg_jieba插件:

git clone https://github.com/jaiminpan/pg_jieba
cd pg_jieba
git submodule update --init --recursive
#此命令如果下载慢的话,可以直接执行`git clone git://github.com/yanyiwu/cppjieba.git`,然后在改名为libjieba
mkdir build
cd build
cmake -DPostgreSQL_TYPE_INCLUDE_DIR=/usr/include/postgresql/10/server/ ..
make
make install
$ psql ladybird -c "CREATE EXTENSION pg_jieba;"

在安装pg_jieba的时候如果报错提示Could NOT find PostgreSQL (missing: PostgreSQL_TYPE_INCLUDE_DIR) (found version "12.3 (Debian 12.3-1.pgdg90+1)"),则需要在cmake的时候指定PostgreSQL_TYPE_INCLUDE_DIR的路劲,如下:

cmake -DPostgreSQL_TYPE_INCLUDE_DIR=/usr/include/postgresql/10/server/ ..