postgis安裝手記

方健發表於2015-05-11

作者:方健

安裝postgresql 9.3

(為什麼裝9.3而不是9.2? 因為yum庫裡9.3以後自帶2.0版的pgrouting,可以以extension形式安裝)

yum localinstall http://yum.postgresql.org/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-1.noarch.rpm 
yum install postgresql93-server

Initialize

service postgresql-9.3 initdb

參考:
http://yum.postgresql.org/repopackages.php
https://wiki.postgresql.org/wiki/YUM_Installation

Startup

chkconfig postgresql-9.3 on
service postgresql-9.3 start

建立使用者

sudo -u postgres createuser --superuser dbuser
sudo -u postgres psql
\password dbuser
\q
sudo -u postgres createdb -O dbuser exampledb

登入問題

vim /var/lib/pgsql/9.3/data/pg_hba.conf
local   all             all                                     trust
host    all             all             127.0.0.1/32            trust
service postgresql-9.3 restart

登入

psql -U dbuser -d exampledb

遠端訪問問題

vim /var/lib/pgsql/9.3/data/pg_hba.conf
host all all 0.0.0.0/0 md5


vi /var/lib/pgsql/9.3/data/postgresql.conf
listen_addresses = '*'

service postgresql-9.3 restart

客戶機:
psql -U dbuser -d exampledb -h dev

控制檯命令

\h:檢視SQL命令的解釋,比如\h select。
\?:檢視psql命令列表。
\l:列出所有資料庫。
\c [database_name]:連線其他資料庫。
\d:列出當前資料庫的所有表格。
\d [table_name]:列出某一張表格的結構。
\du:列出所有使用者。
\e:開啟文字編輯器。
\conninfo:列出當前資料庫和連線的資訊。

資料庫操作

# 建立新表 
CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);
# 插入資料 
INSERT INTO user_tbl(name, signup_date) VALUES('張三', '2013-12-22');
# 選擇記錄 
SELECT * FROM user_tbl;
# 更新資料 
UPDATE user_tbl set name = '李四' WHERE name = '張三';
# 刪除記錄 
DELETE FROM user_tbl WHERE name = '李四' ;
# 新增欄位 
ALTER TABLE user_tbl ADD email VARCHAR(40);
# 更新結構 
ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;
# 更名欄位 
ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;
# 刪除欄位 
ALTER TABLE user_tbl DROP COLUMN email;
# 表格更名 
ALTER TABLE user_tbl RENAME TO backup_tbl;
# 刪除表格 
DROP TABLE IF EXISTS backup_tbl;

參考:
http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html
http://www.cnblogs.com/jlzhou/archive/2013/02/06/2906632.html
http://www.cnblogs.com/terrysun/archive/2012/11/30/2796479.html

安裝PostGis

yum install postgis2_93
su postgres
psql exampledb
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
\q

檢視PostGis版本

SELECT PostGIS_full_version();

PostGis 入門練習

http://workshops.boundlessgeo.com/postgis-intro/

PostGis 文件和cheatsheet

http://www.postgis.net/documentation
http://www.postgis.us/downloads/postgis21_cheatsheet.pdf

PostGis API 參考

http://postgis.net/docs/reference.html

shp匯入到資料庫

* 用pgShapeLoader匯入到Postgrel中  
 username: postgres
 password: postgres
 Server host: 42.121.121.80 :5432
 Database : kjg
 SRID改為4326

參考:http://workshops.boundlessgeo.com/postgis-intro/loading_data.html

* 或者命令列:
 shp2pgsql -s 4326 neighborhoods public.neighborhoods | psql -h myserver -d mydb -U myuser

參考:http://www.bostongis.com/pgsql2shp_shp2pgsql_quickguide.bqg

* 或者QGIS:
 資料庫=》資料庫管理器=》匯入圖層  
 (左側工具欄選“大象”,增加資料庫)  

安裝PgRouting

yum search pgrouting yum install pgrouting_93

增加 pgRouting 功能

su postgres
psql  -d exampledb
CREATE EXTENSION pgrouting;

參考:
http://workshop.pgrouting.org/chapters/installation.html#add-pgrouting-functions-to-database
http://pgrouting.org/docs/1.x/install_centos.html

pgRouting workshop

http://workshop.pgrouting.org/

參考: http://www.cnblogs.com/jlzhou/archive/2013/02/06/2906632.html
http://askubuntu.com/questions/413585/postgres-password-authentication-fails
http://stackoverflow.com/questions/18664074/getting-error-peer-authentication-failed-for-user-postgres-when-trying-to-ge

參考:
http://www.cnblogs.com/sillyemperor/archive/2012/02/16/2354243.html http://www.ruanyifeng.com/blog/2013/12/getting_started_with_postgresql.html

相關文章