Linux中PostgreSQL和PostGIS的安裝和使用方法

本文主要和大家介紹linuxpostgresql和postgis的安裝和使用,并把需要注意點(diǎn)做了分析和解釋?zhuān)枰呐笥褜W(xué)習(xí)下,希望能幫助到大家。

安裝 PostgreSQL 和 PostGIS

PostgreSQL 和 PostGIS 已經(jīng)是熱門(mén)的開(kāi)源工程,已經(jīng)收錄在各大 linux 發(fā)行版的 yum 或 apt 包中。ubuntu 為例,安裝以下包即可:

$?sudo?apt-get?install?postgresql-client?postgresql?postgis?-y

RedHat 系列則請(qǐng)安裝:

$?sudo?yum?install?postgresql-server?postgresql?postgis

初次安裝后,默認(rèn)生成一個(gè)名為 postgres 的數(shù)據(jù)庫(kù)和一個(gè)名為 postgres 的數(shù)據(jù)庫(kù)用戶(hù)。這里需要注意的是,同時(shí)還生成了一個(gè)名為 postgres 的 Linux 系統(tǒng)用戶(hù)。我們以后在操作 PostgreSQL 的時(shí)候都應(yīng)該在這個(gè)新創(chuàng)建的 postgres 用戶(hù)中進(jìn)行。

PostgreSQL 配置

如果是從源碼安裝

不建議從源碼安裝,我曾經(jīng)試過(guò)從源碼安裝,實(shí)在是太麻煩了,而且各種 make install 容易出錯(cuò)。最后我還是用 rpm 安裝了。不過(guò)既然花了些時(shí)間研究并且我成功安裝過(guò),所以還是記錄一下吧——不過(guò),可能有錯(cuò)漏,所以讀者如果要從源碼安裝的話(huà),請(qǐng)做好回滾的準(zhǔn)備。

如果使用的是通過(guò) source 編譯并且 make install 安裝,那么這一節(jié)是需要額外配置的。

貌似 centos 系列的安裝也需要……

默認(rèn)的 make install 之后,PostgreSQL 安裝目錄在:/usr/local/pgsql/

首先根據(jù)這個(gè)鏈接的參考,需要配置環(huán)境變量

$?set?$PGDATA?=?"/usr/local/pgsql/database"

但是執(zhí)行了 pg_ctl start 之后,會(huì)出現(xiàn)錯(cuò)誤:

pg_ctl:?directory?"/usr/local/pgsql/database"?is?not?a?database?cluster?directory

這樣的話(huà),就需要參照 PostGreSQL 官方文檔的步驟創(chuàng)建真正的 database:

PostgreSQL: Documentation: 9.1: Creating a Database Cluster

首先創(chuàng)建一個(gè)用戶(hù)賬戶(hù),名叫 postgres

$?usradd?postgres  $?sudo?chown?postgres?/usr/local/pgsql/database

然后進(jìn)入這個(gè)賬戶(hù),創(chuàng)建 database

$?sudo?su?postgres  $?initdb?-D?/usr/local/pgsql/database/

此時(shí) shell 會(huì)輸出:

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

恭喜你,接下來(lái)就可以啟動(dòng) PostgreSQL 了:

pg_ctl?-D?/usr/local/pgsql/database/?-l?/usr/local/pgsql/database/psql.log?start

PostgreSQL 安裝好后

進(jìn)入 postgres 賬戶(hù),并且進(jìn)入 PostgreSQL 控制臺(tái):

$?sudo?su?postgres  $?psql

這時(shí)相當(dāng)于系統(tǒng)用戶(hù) postgres 以同名數(shù)據(jù)庫(kù)用戶(hù)的身份,登錄數(shù)據(jù)庫(kù),否則我們每次執(zhí)行 psql 的時(shí)候都要在參數(shù)中指定用戶(hù),容易忘。

在 psql 中設(shè)置一下密碼——需要注意的是,這里設(shè)置的密碼并不是 postgres 系統(tǒng)帳戶(hù)的密碼,而是在數(shù)據(jù)庫(kù)中的用戶(hù)密碼:

postgres=#?password?postgres

然后按照提示輸入密碼就好。

從源碼安裝 PostGIS

如果選擇了從源碼安裝 PostgreSQL 的話(huà),那么首先需要判斷你安裝的 PostgreSQL 是什么版本

然后,再到 PostGIS 的網(wǎng)頁(yè)上去查其對(duì)應(yīng)的是 PostGIS 的哪個(gè)版本。

最后,按照 PostGIS 的版本去下載對(duì)應(yīng)的 source

最后的導(dǎo)入很麻煩,筆者就是卡在這一步,所以才最終放棄從源碼安裝的……

導(dǎo)入 PostGIS 擴(kuò)展

根據(jù) postgresql 和 postgis 的版本不同,路徑會(huì)有些差異,主要是路徑中包含版本信息:

$?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)建了一個(gè)叫做 “template_postgis” 的空數(shù)據(jù)庫(kù)。這個(gè)數(shù)據(jù)庫(kù)是空的,并且屬于 postgres 用戶(hù)。注意,不要往這個(gè)數(shù)據(jù)庫(kù)中添加數(shù)據(jù),這個(gè)數(shù)據(jù)庫(kù)之所以稱(chēng)為 “模板”(template),就說(shuō)明它是用來(lái)派生用的。

相應(yīng)的 PostGIS 路徑可能不同,如果失敗,就在上面的路徑附近多嘗試一下,找?guī)讉€(gè) .sql 文件試試看。

轉(zhuǎn)換 .shp 文件到 PostGIS 數(shù)據(jù)庫(kù)中

轉(zhuǎn)換 .shp 到 .sql 文件

首先找到需要轉(zhuǎn)換的文件,假設(shè)需要轉(zhuǎn)換的 .shp 文件是:/tmp/demo.shp,那么就做以下操作:

$?sudo?su?postgres  $?cd?/tmp  $?shp2pgsql?-W?GBK?-s?3857?./demo.shp?entry?>?demo.sql

這里需要說(shuō)明一下最后一句各部分所代表的含義:

  • -W GBK:如果你的 .shp 文件包含中文字符,那么請(qǐng)加上這個(gè)選項(xiàng)

  • -s 3857:指明文件的參考坐標(biāo)系統(tǒng)。我的 .shp 文件使用的是 EPSG:3857

  • ./demo.shp:.shp 文件的路徑

  • entry:表示要導(dǎo)入的數(shù)據(jù)庫(kù)表名——假設(shè)這個(gè) .shp 文件表示的是各個(gè)入口,所以我命名為 “entry”

  • demo.sql

得到了 .sql 文件后,就可以直接導(dǎo)入到 PostgreSQL 數(shù)據(jù)庫(kù)了。

創(chuàng)建一個(gè) PostGIS 數(shù)據(jù)庫(kù)

這里就需要用到前面的 template 了。

sudo?su?postgres  psql  CREATE?DATABASE?newdb?WITH?TEMPLATE?originaldb?OWNER?dbuser;
  • newdb: 新的數(shù)據(jù)庫(kù)名

  • originaldb:也就是前面的 template_postgis

  • dbuser:你的賬戶(hù)名,我一般使用 postgres

導(dǎo)入 .sql 文件

sudo?su?postgres  psql  c?newdb  i?demo.sql  d

可以看到,.sql 文件已經(jīng)被導(dǎo)入了。

設(shè)置數(shù)據(jù)庫(kù)權(quán)限

OK,現(xiàn)在我們?cè)诒緳C(jī)(服務(wù)器 IP 假設(shè)是 192.168.1.111)用以下命令登錄 psql,會(huì)發(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?

這是因?yàn)?PostgreSQL 默認(rèn)不對(duì)外開(kāi)放權(quán)限,只對(duì)監(jiān)聽(tīng)環(huán)回地址。要修改的話(huà),需要找到 postgresql.conf 文件,修改值 listen_addresses:

listen_addresses?=?'*'

相關(guān)推薦:

Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法

Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法

Python連接PostgreSQL數(shù)據(jù)庫(kù)的方法

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊15 分享