國(guó)產(chǎn)系統(tǒng)安裝postgresql+postgis

最近因?yàn)楣ぷ餍枰诙鄠€(gè)國(guó)產(chǎn)操作系統(tǒng)服務(wù)器上安裝了postgresql11.5+postgis3.0.5(鯤鵬、統(tǒng)信uos,中標(biāo)麒麟,銀河麒麟),在整個(gè)過程中遇到了不少問題。

一路走來,雖說踩了各種奇奇怪怪的坑,但也的確學(xué)到了不少東西,在此總結(jié)記錄一下安裝過程和遇到的各種問題,給各位伙伴提供力所能及的幫助,也提供一些解決問題的思路。

全文盡可能多使用離線安裝的方式(原諒我有些地方偷懶了,選擇了在線安裝)。

一、postgresql11.5安裝

#進(jìn)入到軟件包存儲(chǔ)目錄

cd /home/

#解壓安裝包

tar -xjf postgresql-11.5.tar.bz2

#進(jìn)入安裝包所在目錄

cd postgresql-11.5/

#開始安裝配置

./configure

##以下為控制臺(tái)輸出結(jié)果

checking build system type... x86_64-pc-linux-gnu

checking host system type... x86_64-pc-linux-gnu

checking which template to use... linux

checking whether NLS is wanted... no

checking for default port number... 5432

checking for block size... 8kB

checking for segment size... 1GB

checking for WAL block size... 8kB

checking for gcc... no

checking for cc... no

configure: error: in `/home/postgre11.5/postgresql-11.5':

configure: error: no acceptable C compiler found in $PATH

See `config.log' for more details

#提示$PATH中找不到可接受的C編譯器

可能是系統(tǒng)自帶的gcc版本太低或者系統(tǒng)沒有安裝gcc。

如果有外網(wǎng)環(huán)境,可以直接在線安裝

yum install gcc

#繼續(xù)編譯postgresql

./configure

#提示找不到readline庫

configure: error: readline library not found

If you have readline already installed, see config.log for details on the

failure. ?It is possible the compiler isn't looking in the proper directory.

Use --without-readline to disable readline support.

#需要安裝readline

yum install readline-devel -y

可能會(huì)提示glibc32-2.20-7.2.x86_64 和 glibc-2.17-292.el7.ns7.02.i686沖突,導(dǎo)致readline無法安裝(麒麟系統(tǒng)問題)

更新yum(過程會(huì)比較長(zhǎng),耐心等待)

yum update(麒麟系統(tǒng)才會(huì)遇到的問題)

繼續(xù)編譯postgresql

./configure

提示缺少zlib

configure: error: zlib library not found

If you have zlib already installed, see config.log for details on the

failure. ?It is possible the compiler isn't looking in the proper directory.

use --without-zlib to disable zlib support.

#安裝zlib

yum install zlib-devel

繼續(xù)編譯postgresql(過程會(huì)比較長(zhǎng),耐心等待)

./configure

make

提示編譯成功

開始安裝

make install

postgresql被成功安裝,并且根據(jù)輸出信息可能看到postgresql被默認(rèn)安裝在/usr/local/pgsql目錄下

#設(shè)置數(shù)據(jù)存儲(chǔ)目錄

mkdir -p /usr/local/pgsql/pgdata

#添加用戶,并給權(quán)限改為postgres

useradd postgres

chown -R postgres:postgres /usr/local/pgsql/

#編輯環(huán)境變量文件

vim /etc/profile

#環(huán)境變量配置

export PATH=/usr/local/pgsql/bin:$PATH

LD_LIBRARY_PATH=/usr/local/pgsql/lib

export LD_LIBRARY_PATH

#重載配置讓環(huán)境變量設(shè)置生效

source /etc/profile

#切換到postgres用戶,并初始化數(shù)據(jù)庫

su - postgres

initdb -D??/usr/local/pgsql/pgdata

根據(jù)提示我們可以用以下命令啟動(dòng)服務(wù)

pg_ctl -D /usr/local/pgsql/pgdata -l logfile start


#設(shè)置數(shù)據(jù)庫密碼

切換到postgres用戶:

su postgres

啟動(dòng)psql:

psql

執(zhí)行修改密碼的sql語句:

ALTER USER postgres WITH PASSWORD 'postgres';


設(shè)置遠(yuǎn)程連接

#需要修改PostgreSQL的連接參數(shù),允許遠(yuǎn)程連接。

1、修改/usr/local/pgsql/pgdata/postgresql.conf

將#listen_addresses = 'localhost'修改成:listen_addresses = '*'

將#port = 5432修改成:port = 5432

2、修改/usr/local/pgsql/pgdata/pg_hba.conf

添加下列行

host ???all all ? 0.0.0.0/0 ? md5

postgresql.conf和pg_hba.conf文件是在做初始化數(shù)據(jù)庫的時(shí)候自動(dòng)生成的文件,如果不知道這兩個(gè)配置文件在哪里,也可以使用以下命令查找文件位置。

find / -name?postgresql.conf

find / -name?pg_hba.conf

全局檢索命令,使用find / -name *(*號(hào)表示文件全名)


數(shù)據(jù)庫的啟動(dòng),停止和重啟

systemctl start postgresql

systemctl stop postgresql

systemctl restart postgresql

也可以用以下方法啟動(dòng)數(shù)據(jù)庫。

su postgres

pg_ctl -D /usr/local/pgsql/pgdata -l logfile start

pg_ctl -D /usr/local/pgsql/pgdata -l logfile stop

pg_ctl -D /usr/local/pgsql/pgdata -l logfile restart


#設(shè)置開機(jī)自啟動(dòng),并將postgresql添加系統(tǒng)服務(wù)

postgresql的自啟動(dòng)文件為postgresql/contrib/start-scripts編譯目錄下的linux文件

cp postgresql/contrib/start-scripts/linux /etc/rc.d/init.d/

將文件重命名為postgresql

mv /etc/rc.d/init.d/linux /etc/rc.d/init.d/postgresql

設(shè)置postgresql的安裝目錄和數(shù)據(jù)目錄

# Installation prefix(數(shù)據(jù)庫安裝目錄)

prefix=/usr/local/pgsql

# Data directory(數(shù)據(jù)庫文件存儲(chǔ)目錄)

PGDATA="/usr/local/pgsql/pgdata"

加執(zhí)行權(quán)限

chmod +x /etc/rc.d/init.d/postgresql

添加到服務(wù)

chkconfig --add postgresql



二、postgis3.0.5安裝

1、安裝libxml2-2.9.9

tar -xzvf libxml2-2.9.9.tar.gz

cd libxml2-2.9.9

./configure

make

make install


2、安裝 geos-3.8.0

tar -xjf geos-3.8.0.tar.bz2

cd geos-3.8.0

./configure

make

make install


3、安裝 proj-6.2.1

./configure

#提示sqlite3版本太低

#因?yàn)闇y(cè)試服務(wù)器有網(wǎng)絡(luò)環(huán)境,所以此處直接在線安裝

yum install sqlite-devel

tar -xzvf?proj-6.2.1.tar.gz

cd?proj-6.2.1

./configure

make

make install


4、安裝gdal-3.3.3

tar -xzvf?gdal-3.3.3.tar.gz

cd?gdal-3.3.3

./configure()

make

make install

提示缺少jasper,導(dǎo)致無法安裝gdal


5、安裝jasper

tar -xzvf?jasper-1.900.1.uuid.tar.gz

cd?jasper-1.900.1.uuid

./configure --enable-shared?

make

make install

我安裝此依賴的時(shí)候都是默認(rèn)安裝,最開始沒有加上生產(chǎn)動(dòng)態(tài)鏈接庫的參數(shù),可以看到/usr/local/lib目錄下沒有生成動(dòng)態(tài)鏈接文件,從而導(dǎo)致gdal無法成功被安裝。

有興趣的伙伴可以點(diǎn)這里,了解一下其原理。

簡(jiǎn)單來講,?linux中,靜態(tài)庫的命名規(guī)則通常為lib*.a? ? ? ? 動(dòng)態(tài)庫的命名通常是*.so


5、安裝ogr-fdw-1.0.6

tar -xzf pgsql-ogr-fdw-1.0.6.tar.gz

cd /home/pgsql-ogr-fdw-1.0.6

make clean

make && make install


6、安裝SFCGAL (可選)

6.1.安裝cmake

unzip CMake-3.21.1.zip

cd CMake-3.21.1

make

make install


6.2.安裝sfcgal 依賴 boost,cgal2.6.3.

#安裝boost

yum -y install boost-devel

#安裝cgal

tar -zxvf cgal-releases-CGAL-4.13.tar.gz

cd cgal-releases-CGAL-4.13

mkdir build && cd build

cmake ..

make

make install

#6.3可能出現(xiàn)缺少GMP和MPFR的情況,導(dǎo)致cgal無法被安裝

cd ?/home/postgis/cgal-releases-CGAL-4.13/build

cmake ..

make

make install

安裝GMP用以編譯cgal

tar xvf gmp-6.2.1.tar

cd gmp-6.2.1

./configure

make && make install

安裝MPFR用以編譯cgal

tar -xzvf mpfr-4.1.0.tar.gz

cd mpfr-4.1.0

./configure

make && make install


7、安裝fuzzystrmatch

cd /home/postgresql-11.5/contrib/fuzzystrmatch

make && make install


8、安裝 postgis

./configure

make

make install


9、新建GIS擴(kuò)展

CREATE EXTENSION postgis;

CREATE EXTENSION postgis_topology;

CREATE EXTENSION ogr_fdw;

CREATE EXTENSION postgis_raster;

CREATE EXTENSION postgis_sfcgal;

CREATE EXTENSION fuzzystrmatch;

CREATE EXTENSION address_standardizer;

CREATE EXTENSION address_standardizer_data_us;

CREATE EXTENSION postgis_tiger_geocoder;

注意:根據(jù)自身的業(yè)務(wù)需求,可選擇性安裝部分?jǐn)U展。以上擴(kuò)展不是必須全部安裝的。


三、記錄在創(chuàng)建GIS擴(kuò)展時(shí),可能會(huì)遇到的問題

問題1:找不到相關(guān)鏈接庫文件1

#進(jìn)入到缺少文件的目錄下

cd /usr/local/pgsql/lib/

#查看缺少哪些依賴

ldd postgis-3.so

可以看到?libgeos_c.so.1和libproj.so.12是not found。

[root@linkgis lib]# find / -name libproj.so.12

/usr/local/lib/libproj.so.12

[root@linkgis lib]# find / -name libgeos_c.so.1

/usr/local/lib/libgeos_c.so.1

[root@linkgis lib]# cp /usr/local/lib/libproj.so.12 /usr/local/pgsql/lib/

[root@linkgis lib]# cp /usr/local/lib/libgeos_c.so.1 /usr/local/pgsql/lib/


問題 2: PostGIS二進(jìn)制文件是使用與安裝的版本不兼容的GEOS版本構(gòu)建的,版本沖突導(dǎo)致無法創(chuàng)建擴(kuò)展。

ERROR: could not load library "/usr/local/pgsql/lib/postgis-3.so": /usr/local/pgsql/lib/postgis-3.so: undefined symbol: GEOSCoordSeq_getXY

#根據(jù)提示進(jìn)入相關(guān)目錄

cd /usr/local/pgsql/lib/

#通過ldd命令,查看數(shù)據(jù)庫正在使用哪個(gè)libgeos_c.so.1文件

ldd postgis-3.so

因?yàn)樯厦嬉呀?jīng)提示了geos的版本不兼容,所以找到服務(wù)器上已存在的libgeos_c.so.1文件,并刪除所有的libgeos_c.so.1(這個(gè)文件是編譯安裝geos時(shí)自動(dòng)生成的,無需擔(dān)心無法恢復(fù))。

查找服務(wù)器上是否還存在libgeos_c.so.1文件,如果還存在該文件,繼續(xù)刪除,直至找不到該文件為止。然后再用ldd?postgis-3.so命令查看postgis引用文件已為not found。至此,已經(jīng)完全刪除了與postgis版本不兼容的文件。

ldd postgis-3.so

注:如果用我目錄里面指定的版本不會(huì)出現(xiàn)這種問題。

./configure

make

make install

#使用命令查找安裝geos后生成的libgeos_c.so.1文件,并將新生成的文件拷貝到相應(yīng)目錄下

find / -name libgeos_c.so.1

cp /usr/local/lib/libgeos_c.so.1 /usr/local/pgsql/lib/






今天安裝aarch64架構(gòu)服務(wù)器又遇到了一個(gè)新的坑

現(xiàn)象:創(chuàng)建postgis擴(kuò)展的時(shí)候,一直提示缺這個(gè)文件,但是我安裝postgis的時(shí)候提示讀到這個(gè)版本了。并且我也把缺失的文件拷貝到了我對(duì)應(yīng)的目錄下。但是問題一直沒得到解決。

最后求助華為工程師,最可以看一下華為工程師的解決思路。

幫我從新參照鏈接安裝了geos和gdal,并且設(shè)置了環(huán)境變量,還給了相關(guān)目錄更高的權(quán)限。解決了我的問題。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容