本文主要和大家介紹linux中postgresql和postgis的安裝和使用,并把需要注意點做了分析和解釋,需要的朋友學習下,希望能幫助到大家。
安裝 PostgreSQL 和 PostGIS
PostgreSQL 和 PostGIS 已經(jīng)是熱門的開源工程,已經(jīng)收錄在各大 linux 發(fā)行版的 yum 或 apt 包中。ubuntu 為例,安裝以下包即可:
$?sudo?apt-get?install?postgresql-client?postgresql?postgis?-y
RedHat 系列則請安裝:
$?sudo?yum?install?postgresql-server?postgresql?postgis
初次安裝后,默認生成一個名為 postgres 的數(shù)據(jù)庫和一個名為 postgres 的數(shù)據(jù)庫用戶。這里需要注意的是,同時還生成了一個名為 postgres 的 Linux 系統(tǒng)用戶。我們以后在操作 PostgreSQL 的時候都應該在這個新創(chuàng)建的 postgres 用戶中進行。
PostgreSQL 配置
如果是從源碼安裝
不建議從源碼安裝,我曾經(jīng)試過從源碼安裝,實在是太麻煩了,而且各種 make install 容易出錯。最后我還是用 rpm 安裝了。不過既然花了些時間研究并且我成功安裝過,所以還是記錄一下吧——不過,可能有錯漏,所以讀者如果要從源碼安裝的話,請做好回滾的準備。
如果使用的是通過 source 編譯并且 make install 安裝,那么這一節(jié)是需要額外配置的。
貌似 centos 系列的安裝也需要……
默認的 make install 之后,PostgreSQL 安裝目錄在:/usr/local/pgsql/
首先根據(jù)這個鏈接的參考,需要配置環(huán)境變量
$?set?$PGDATA?=?"/usr/local/pgsql/database"
但是執(zhí)行了 pg_ctl start 之后,會出現(xiàn)錯誤:
pg_ctl:?directory?"/usr/local/pgsql/database"?is?not?a?database?cluster?directory
這樣的話,就需要參照 PostGreSQL 官方文檔的步驟創(chuàng)建真正的 database:
PostgreSQL: Documentation: 9.1: Creating a Database Cluster
首先創(chuàng)建一個用戶賬戶,名叫 postgres
$?usradd?postgres $?sudo?chown?postgres?/usr/local/pgsql/database
然后進入這個賬戶,創(chuàng)建 database
$?sudo?su?postgres $?initdb?-D?/usr/local/pgsql/database/
此時 shell 會輸出:
The?files?belonging?to?this?database?system?will?be?owned?by?user?"postgres". This?user?must?also?own?the?server?process. The?database?cluster?will?be?initialized?with?locale?"C". The?default?database?encoding?has?accordingly?been?set?to?"SQL_ASCII". The?default?text?search?configuration?will?be?set?to?"english". Data?page?checksums?are?disabled. fixing?permissions?on?existing?directory?/usr/local/pgsql/database?...?ok creating?subdirectories?...?ok selecting?default?max_connections?...?100 selecting?default?shared_buffers?...?128MB selecting?dynamic?shared?memory?implementation?...?posix creating?configuration?files?...?ok creating?template1?database?in?/usr/local/pgsql/database/base/1?...?ok initializing?pg_authid?...?ok initializing?dependencies?...?ok creating?system?views?...?ok loading?system?objects'?descriptions?...?ok creating?collations?...?ok creating?conversions?...?ok creating?dictionaries?...?ok setting?privileges?on?built-in?objects?...?ok creating?information?schema?...?ok loading?PL/pgSQL?server-side?language?...?ok vacuuming?database?template1?...?ok copying?template1?to?template0?...?ok copying?template1?to?postgres?...?ok syncing?data?to?disk?...?ok WARNING:?enabling?"trust"?authentication?for?local?connections You?can?change?this?by?editing?pg_hba.conf?or?using?the?option?-A,?or --auth-local?and?--auth-host,?the?next?time?you?run?initdb. Success.?You?can?now?start?the?database?server?using: pg_ctl?-D?/usr/local/pgsql/database/?-l?logfile?start
恭喜你,接下來就可以啟動 PostgreSQL 了:
pg_ctl?-D?/usr/local/pgsql/database/?-l?/usr/local/pgsql/database/psql.log?start
PostgreSQL 安裝好后
進入 postgres 賬戶,并且進入 PostgreSQL 控制臺:
$?sudo?su?postgres $?psql
這時相當于系統(tǒng)用戶 postgres 以同名數(shù)據(jù)庫用戶的身份,登錄數(shù)據(jù)庫,否則我們每次執(zhí)行 psql 的時候都要在參數(shù)中指定用戶,容易忘。
在 psql 中設置一下密碼——需要注意的是,這里設置的密碼并不是 postgres 系統(tǒng)帳戶的密碼,而是在數(shù)據(jù)庫中的用戶密碼:
postgres=#?password?postgres
然后按照提示輸入密碼就好。
從源碼安裝 PostGIS
如果選擇了從源碼安裝 PostgreSQL 的話,那么首先需要判斷你安裝的 PostgreSQL 是什么版本
然后,再到 PostGIS 的網(wǎng)頁上去查其對應的是 PostGIS 的哪個版本。
最后,按照 PostGIS 的版本去下載對應的 source
最后的導入很麻煩,筆者就是卡在這一步,所以才最終放棄從源碼安裝的……
導入 PostGIS 擴展
根據(jù) postgresql 和 postgis 的版本不同,路徑會有些差異,主要是路徑中包含版本信息:
$?sudo?su?postgres $?createdb?template_postgis $?createlang?plpgsql?template_postgis $?psql?-d?template_postgis?-f?/usr/share/postgresql/9.5/contrib/postgis-2.2/postgis.sql $?psql?-d?template_postgis?-f?/usr/share/postgresql/9.5/contrib/postgis-2.2/spatial_ref_sys.sql
上面的操作中,創(chuàng)建了一個叫做 “template_postgis” 的空數(shù)據(jù)庫。這個數(shù)據(jù)庫是空的,并且屬于 postgres 用戶。注意,不要往這個數(shù)據(jù)庫中添加數(shù)據(jù),這個數(shù)據(jù)庫之所以稱為 “模板”(template),就說明它是用來派生用的。
相應的 PostGIS 路徑可能不同,如果失敗,就在上面的路徑附近多嘗試一下,找?guī)讉€ .sql 文件試試看。
轉(zhuǎn)換 .shp 文件到 PostGIS 數(shù)據(jù)庫中
轉(zhuǎn)換 .shp 到 .sql 文件
首先找到需要轉(zhuǎn)換的文件,假設需要轉(zhuǎn)換的 .shp 文件是:/tmp/demo.shp,那么就做以下操作:
$?sudo?su?postgres $?cd?/tmp $?shp2pgsql?-W?GBK?-s?3857?./demo.shp?entry?>?demo.sql
這里需要說明一下最后一句各部分所代表的含義:
-
-W GBK:如果你的 .shp 文件包含中文字符,那么請加上這個選項
-
-s 3857:指明文件的參考坐標系統(tǒng)。我的 .shp 文件使用的是 EPSG:3857
-
./demo.shp:.shp 文件的路徑
-
entry:表示要導入的數(shù)據(jù)庫表名——假設這個 .shp 文件表示的是各個入口,所以我命名為 “entry”
-
demo.sql
得到了 .sql 文件后,就可以直接導入到 PostgreSQL 數(shù)據(jù)庫了。
創(chuàng)建一個 PostGIS 數(shù)據(jù)庫
這里就需要用到前面的 template 了。
sudo?su?postgres psql CREATE?DATABASE?newdb?WITH?TEMPLATE?originaldb?OWNER?dbuser;
-
newdb: 新的數(shù)據(jù)庫名
-
originaldb:也就是前面的 template_postgis
-
dbuser:你的賬戶名,我一般使用 postgres
導入 .sql 文件
sudo?su?postgres psql c?newdb i?demo.sql d
可以看到,.sql 文件已經(jīng)被導入了。
設置數(shù)據(jù)庫權限
OK,現(xiàn)在我們在本機(服務器 IP 假設是 192.168.1.111)用以下命令登錄 psql,會發(fā)現(xiàn)一段輸出:
$?psql?-h?192.168.1.111?-p?5432 psql:?could?not?connect?to?server:?Connection?refused ????Is?the?server?running?on?host?"100.94.110.105"?and?accepting ????TCP/IP?connections?on?port?5432?
這是因為 PostgreSQL 默認不對外開放權限,只對監(jiān)聽環(huán)回地址。要修改的話,需要找到 postgresql.conf 文件,修改值 listen_addresses:
listen_addresses?=?'*'
相關推薦:
Python連接PostgreSQL數(shù)據(jù)庫的方法