最近在做GIS分析,采集設備的經(jīng)緯度點判斷是否進出圍欄以及是否產(chǎn)生道路偏移報警,在之前的文章有介紹過Windows下使用C#來實現(xiàn),參考文章:利用Postgresql+Postgis進行空間地理信息分析(道路偏移,進出電子圍欄等)_postgres 空間分析_大魚>的博客-CSDN博客最近新做了一個物聯(lián)網(wǎng)項目,采用的是Springboot+mongoDB+MySQL,所以考慮使用Linux服務器重新實現(xiàn)這個服務。此文只介紹Ubuntu下安裝Postgresql與PostGIS環(huán)境。
2.安裝Postgresql
2.1.查看apt-get庫中的軟件版本支持
執(zhí)行命令:
sudo apt-cache search postgresql
我這邊使用的是ubuntu 18.04,查看后發(fā)現(xiàn)目前支持postgresql-10
2.2.執(zhí)行安裝命令
sudo apt-get install postgresql-10
2.3.postgresql-common not configured yet.錯誤處理
執(zhí)行安裝命令后發(fā)現(xiàn)postgresql-common不能安裝成功,查看報錯信息為:postgresql-common not configured yet.
我們查看/var/lib/dpkg/status文件,找到Package:postgresql-common
如果其Status: install ok half-configured則將其改為install ok installed
然后再執(zhí)行安裝命令,如果是上述情況,此時應該是可以安裝成功了。
3.安裝postgis
執(zhí)行安裝腳本:
sudo apt-get install postgis
4.修改數(shù)據(jù)庫默認用戶名、密碼
PostgreSQL數(shù)據(jù)庫創(chuàng)建一個postgres用戶作為數(shù)據(jù)庫的管理員,密碼隨機,所以這里需要進行密碼修改操作。
執(zhí)行命令登錄PostgreSQL:
sudo -u postgres psql
執(zhí)行命令修改登錄PostgreSQL密碼:
alter user postgres with password '你的密碼';
安裝PostgreSQL后,會給創(chuàng)建一個Linux用戶,這個用戶一定要馬上重置密碼,否則很容易導致服務器被攻擊,我的服務器之前就因為這個密碼沒有修改被安裝了挖礦程序,費了很大勁才徹底清掉挖礦程序。
執(zhí)行刪除原有用戶密碼的命令:
sudo passwd -d postgres
設置新密碼命令:
sudo -u postgres passwd
隨后系統(tǒng)會提示輸入新密碼:
Enter new UNIX password:
確認密碼:
Retype new UNIX password:
最后提示passwd: password updated successfully,代表完成了密碼的修改。
5.創(chuàng)建數(shù)據(jù)庫并添加postgis
切換Linux:postgres用戶
sudo su postgres
創(chuàng)建名一個數(shù)據(jù)庫,根據(jù)自己的需要起名字
createdb postgis_24_sample
為數(shù)據(jù)庫添加postgis支持。
腳本默認在/usr/share/postgresql目錄下,自己可以找找
如果找不到上述目錄文件,說明postgis安裝失敗或者先安裝的postgis后安裝的postgresql,所以卸載postgis服務,重新安裝即可。
卸載方法:
sudo apt-get purge 'postgis*' sudo apt-get autoremove 'postgis*
然后執(zhí)行sql腳本
psql -d postgis_24_sample -f /usr/share/postgresql/10/contrib/postgis-2.4/postgis.sql psql -d postgis_24_sample -f /usr/share/postgresql/10/contrib/postgis-2.4/spatial_ref_sys.sql
如果過程中提示下面的錯誤:
psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
可以重啟一下postgres服務,執(zhí)行重啟命令:
sudo service postgres restart
6.使用pgAdmin4連接數(shù)據(jù)庫
我們可以使用pgAdmin4連接到服務器上的postgresql數(shù)據(jù)庫,如果報下面的錯誤,則確認一下服務是否開啟了遠程連接。
錯誤提示:could not connect to server: Connection refused (0x0000274D/10061) Is the server running on host "124.223.60.234" and accepting TCP/IP connections on port 5432?
修改配置文件postgresql.conf:
sudo vim /etc/postgresql/10/main/postgresql.conf
如果listen_addresses 被注釋掉,則去掉#注釋,并將值修改為*
另外修改pg_hba.conf
sudo vim /etc/postgresql/10/main/pg_hba.conf
在文件中添加 host all all 0.0.0.0/0 trust
最后登錄成功
考慮到安全,建議上述如果非必要不要對所有外網(wǎng)都開啟訪問,可以指定對應的IP或者只允許內(nèi)網(wǎng)訪問,否則很容易被黑。導致postgresql總是被重啟。
原文鏈接:
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。