Linux中PostgreSQL和PostGIS的安裝與使用方法
本文主要和大家介紹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:
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
#
$ 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
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 = '*'
相关推荐:
以上是Linux中PostgreSQL和PostGIS的安裝與使用方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Linux系統的五個基本組件是:1.內核,2.系統庫,3.系統實用程序,4.圖形用戶界面,5.應用程序。內核管理硬件資源,系統庫提供預編譯函數,系統實用程序用於系統管理,GUI提供可視化交互,應用程序利用這些組件實現功能。

VS Code 一步/下一步快捷鍵的使用方法:一步(向後):Windows/Linux:Ctrl ←;macOS:Cmd ←下一步(向前):Windows/Linux:Ctrl →;macOS:Cmd →

要查看 Git 倉庫地址,請執行以下步驟:1. 打開命令行並導航到倉庫目錄;2. 運行 "git remote -v" 命令;3. 查看輸出中的倉庫名稱及其相應的地址。

雖然 Notepad 無法直接運行 Java 代碼,但可以通過借助其他工具實現:使用命令行編譯器 (javac) 編譯代碼,生成字節碼文件 (filename.class)。使用 Java 解釋器 (java) 解釋字節碼,執行代碼並輸出結果。

在 Sublime 中運行代碼的方法有六種:通過熱鍵、菜單、構建系統、命令行、設置默認構建系統和自定義構建命令,並可通過右鍵單擊項目/文件運行單個文件/項目,構建系統可用性取決於 Sublime Text 的安裝情況。

Linux的主要用途包括:1.服務器操作系統,2.嵌入式系統,3.桌面操作系統,4.開發和測試環境。 Linux在這些領域表現出色,提供了穩定性、安全性和高效的開發工具。

要安裝 Laravel,需依序進行以下步驟:安裝 Composer(適用於 macOS/Linux 和 Windows)安裝 Laravel 安裝器創建新項目啟動服務訪問應用程序(網址:http://127.0.0.1:8000)設置數據庫連接(如果需要)
