记录一些常用语句,随时会增加
postgresql9.4, postgis2.1
1.在postgresql的bin目录下打开命令行,输入:
createdb -U postgres -E UTF8 osm
其中osm是数据库的名称,postgres是数据库的用户名。执行完成后我们打开pgAdmin III客户端刷新,就可以看到刚才创建的数据库。
2.createlang -U postgres plpgsql osm
一般这一步都不需要,默认已经安装上去了。
3.添加PostGIS功能到数据库,执行如下命令:
psql -U postgres -d osm -f “你PostgreSQL安装路径/share/contrib/postgis-2.1/postgis.sql”
控制台会打印如下一些命令:
![在这里插入图片描述](https://img-blog.csdn.net/20160403192317583?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
4.往spatial_ref_sys表中输入你需要使用的空间坐标系,如epsg4326:
INSERT into spatial_ref_sys (srid, auth_name, auth_srid, proj4text, srtext) values ( 4326, 'epsg', 4326, '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ', 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]');
不同坐标系的insert语句可以在这个网站找到 http://spatialreference.org/ref/epsg/4326/
![在这里插入图片描述](https://img-blog.csdn.net/20160403193252461?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
点击PostGIS spatial_ref_sys INSERT statement链接可以得到insert语句(这里有一个问题是主键是94326,在geoserver中不能自动识别,所以手动改成了4326)
![在这里插入图片描述](https://img-blog.csdn.net/20160403193424227?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
CREATE TABLE stations ( id varchar(12) primary key, lonlat geometry(Point,4326), area float8, polygon geometry(Polygon,4326) );
INSERT INTO stations (id, lonlat, area, polygon) VALUES ('A12345',ST_GeomFromText('POINT(121.50 31.22)',4326),22222.1234,ST_GeomFromText('POLYGON ((121.71436091670316 31.38080337674083, 121.70173615072089 31.388605486784197, 121.70751628677645 31.4199972067024, 121.71832867120243 31.437168981489133, 121.73193985623796 31.37984624401758, 121.71436091670316 31.38080337674083))',4326));
select id, area, ST_AsText(lonlat), ST_AsEwkt(polygon) from stations
![在这里插入图片描述](https://img-blog.csdn.net/20160403204447928?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
psql -U postgres(用户名) -d 数据库名(缺省时同用户名) < /data/dum.sql(路径)