国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 服務器 > Linux服務器 > 正文

詳解Linux中PostgreSQL和PostGIS的安裝和使用

2024-09-05 23:04:36
字體:
供稿:網(wǎng)友

安裝 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:<br/>

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 ... okcreating subdirectories ... okselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting dynamic shared memory implementation ... posixcreating configuration files ... okcreating template1 database in /usr/local/pgsql/database/base/1 ... okinitializing pg_authid ... okinitializing dependencies ... okcreating system views ... okloading system objects' descriptions ... okcreating collations ... okcreating conversions ... okcreating dictionaries ... oksetting privileges on built-in objects ... okcreating information schema ... okloading PL/pgSQL server-side language ... okvacuuming database template1 ... okcopying template1 to template0 ... okcopying template1 to postgres ... oksyncing data to disk ... okWARNING: enabling "trust" authentication for local connectionsYou 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 postgrespsqlCREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;
  • newdb: 新的數(shù)據(jù)庫名
  • originaldb:也就是前面的 template_postgis
  • dbuser:你的賬戶名,我一般使用 postgres

導入 .sql 文件

sudo su postgrespsql/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 5432psql: 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 = '*'

以上就是本次小編整理的關于Linux中PostgreSQL和PostGIS的安裝和使用的全部內(nèi)容,感謝你對VEVB武林網(wǎng)的支持。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 本溪市| 永登县| 襄樊市| 漾濞| 静宁县| 五家渠市| 花莲市| 名山县| 忻州市| 侯马市| 十堰市| 博野县| 交口县| 海兴县| 舒兰市| 东宁县| 富锦市| 华亭县| 繁峙县| 商洛市| 邓州市| 塔河县| 隆回县| 施秉县| 龙井市| 九台市| 饶河县| 广宗县| 兴业县| 布拖县| 永平县| 独山县| 上犹县| 岳池县| 东兴市| 龙江县| 岳阳市| 博白县| 郴州市| 河南省| 万州区|