首頁  >  文章  >  運維  >  Linux中PostgreSQL和PostGIS的安裝與使用方法

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

小云云
小云云原創
2018-02-07 09:30:092549瀏覽

本文主要和大家介紹Linux中PostgreSQL和PostGIS的安裝和使用,並把需要注意點做了分析和解釋,需要的朋友學習下,希望能幫助到大家。

安裝 PostgreSQL 和 PostGIS

PostgreSQL 和 PostGIS 已經是熱門的開源工程,已經收錄在各大 Linux 發行版的 yum 或 apt 套件中。 Ubuntu 為例,安裝以下套件即可:


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

RedHat 系列則請安裝:


$ sudo yum install postgresql-server postgresql postgis

初次安裝後,預設產生一個名為postgres 的資料庫和一個名為postgres 的資料庫使用者。這裡要注意的是,同時也產生了一個名為 postgres 的 Linux 系統使用者。我們以後在操作 PostgreSQL 的時候都應該在這個新建立的 postgres 使用者中進行。

PostgreSQL 設定

如果是從原始碼安裝

#不建議從原始碼安裝,我曾經試過從原始碼安裝,實在是太麻煩了,而且各種make install 容易出錯。最後我還是用 rpm 安裝了。不過既然花了些時間研究並且我成功安裝過,所以還是記錄一下吧——不過,可能有錯漏,所以讀者如果要從源碼安裝的話,請做好回滾的準備。

如果使用的是透過 source 編譯並且 make install 安裝,那麼這一節是需要額外設定的。

看起來像CentOS 系列的安裝也需要…

預設的make install 之後,PostgreSQL 安裝目錄在:/usr/local/pgsql/

首先根據這個連結的參考,需要配置環境變數


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

但是執行了pg_ctl start 之後,會出現錯誤:


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

這樣的話,就需要參考PostGreSQL 官方文件的步驟來建立真正的database:076402276aae5dbec7f672f8f4e5cc81

PostgreSQL: Documentation: 9.1: Creating a Database Cluster

首先建立一個使用者帳戶,名叫postgres


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

然後進入這個帳戶,建立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

這時相當於系統使用者postgres 以同名資料庫使用者的身份,登入資料庫,否則我們每次執行psql 的時候都要在參數中指定用戶,容易忘。

在psql 中設定一下密碼-需要注意的是,這裡設定的密碼並不是postgres 系統帳號的密碼,而是在資料庫中的使用者密碼:


postgres=# \password postgres

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

從原始碼安裝PostGIS

如果選擇了從原始碼安裝PostgreSQL 的話,那麼首先需要判斷你安裝的PostgreSQL 是什麼版本

#然後,再到PostGIS的網頁上去查其對應的是PostGIS 的哪個版本。

最後,按照PostGIS 的版本去下載對應的source

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

匯入PostGIS 擴充功能

根據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

上面的操作中,建立了一個叫做「template_postgis」 的空資料庫。這個資料庫是空的,並且屬於 postgres 使用者。注意,不要往這個資料庫中新增數據,這個資料庫之所以稱為 「模板」(template),就說明它是用來派生用的。

對應的 PostGIS 路徑可能不同,如果失敗,就在上面的路徑附近多嘗試一下,找幾個 .sql 檔案試試看。

轉換.shp 檔案到PostGIS 資料庫中


轉換.shp 到.sql 檔案


首先找到需要轉換的文件,假設需要轉換的.shp 檔案是:/tmp/demo.shp,那麼就做以下操作:


#

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

這裡需要說明一下最後一句各部分所代表的含義:

  • -W GBK:如果你的.shp 檔案包含中文字符,那麼請加上這個選項

  • -s 3857:指明檔案的參考座標系統。我的.shp 檔案使用的是EPSG:3857

  • ./demo.shp:.shp 檔案的路徑

  • entry:表示要導入的資料庫表名-假設這個.shp 檔案表示的是各個入口,所以我命名為「entry」

  • demo.sql

得到了.sql 檔案後,就可以直接匯入到PostgreSQL 資料庫了。

建立一個 PostGIS 資料庫


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


sudo su postgres
psql
CREATE DATABASE newdb WITH TEMPLATE originaldb OWNER dbuser;

  • newdb: 新的資料庫名稱

  • originaldb:也就是前面的 template_postgis

  • dbuser:你的账户名,我一般使用 postgres

导入 .sql 文件


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

可以看到,.sql 文件已经被导入了。

设置数据库权限

OK,现在我们在本机(服务器 IP 假设是 192.168.1.111)用以下命令登录 psql,会发现一段输出:


$ 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 默认不对外开放权限,只对监听环回地址。要修改的话,需要找到 postgresql.conf 文件,修改值 listen_addresses:


listen_addresses = '*'

相关推荐:

Python连接PostgreSQL数据库的方法

PHP连接不上PostgreSQL的问题

PHP 读取Postgresql中的数组_PHP教程

以上是Linux中PostgreSQL和PostGIS的安裝與使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn