支持的系统:Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows.
功能:has full support for foreign keys, joins, views, triggers, and stored procedures.
数据类型:It includes most SQL:2008 data types, including INTEGER, NUMERIC, BOOLEAN, CHAR, VARCHAR, DATE, INTERVAL, and TIMESTAMP. It also supports storage of binary large objects, including pictures, sounds, or video.此外,用户可以创建自定义数据类型,通常通过PostgreSQL的GiST机制,它们也能被很好得索引,比如PostGIS地理信息系统的数据类型.
语言支持:It has native programming interfaces for C/C++, Java, .Net, Perl, Python, Ruby, Tcl, ODBC, among others, and exceptional documentation.
2. 登录 psql -h host -U user -d database_name -p port
PostgreSQL默认端口为5432,-d参数指定数据库名称:
1234
xiaobaoqiu@xiaobaoqiu:/var/run/postgresql$ psql -h localhost -U postgres
Password for user postgres:
psql.bin (9.3.4)
Type "help" for help.
3. 退出
1
\q
4. 所有用户
12345
postgis_template=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication | {}
4. 连接信息
12
postgres=# \conninfo
You are connected to database "postgres" as user "postgres" on host "localhost" at port "5432".
4. show databases
12345
hotel=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
---------------------+----------+----------+------------+------------+-----------------------
fb_stat | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
5. switch database
12
postgres=# \c postgis_template
You are now connected to database "postgis_template" as user "postgres".
6. show tables
\d 显示所有表
1234567891011
postgis_template=# \d
List of relations
Schema | Name | Type | Owner
----------+-------------------+----------+----------
public | geography_columns | view | postgres
public | geometry_columns | view | postgres
public | spatial_ref_sys | table | postgres
topology | layer | table | postgres
topology | topology | table | postgres
topology | topology_id_seq | sequence | postgres
(6 rows)
\d table_name
1234567891011
postgis_template=# \d geography_columns
View "public.geography_columns"
Column | Type | Modifiers
--------------------+---------+-----------
f_table_catalog | name |
f_table_schema | name |
f_table_name | name |
f_geography_column | name |
coord_dimension | integer |
srid | integer |
type | text |
# 创建表
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;
8. PostgreSQL Client支持的一些命令(直接在shell运行):
1234567891011121314151617
clusterdb -- cluster a PostgreSQL database
createdb -- create a new PostgreSQL database
createlang -- install a PostgreSQL procedural language
createuser -- define a new PostgreSQL user account
dropdb -- remove a PostgreSQL database
droplang -- remove a PostgreSQL procedural language
dropuser -- remove a PostgreSQL user account
ecpg -- embedded SQL C preprocessor
pg_basebackup -- take a base backup of a PostgreSQL cluster
pg_config -- retrieve information about the installed version of PostgreSQL
pg_dump -- extract a PostgreSQL database into a script file or other archive file
pg_dumpall -- extract a PostgreSQL database cluster into a script file
pg_receivexlog -- streams transaction logs from a PostgreSQL cluster
pg_restore -- restore a PostgreSQL database from an archive file created by pg_dump
psql -- PostgreSQL interactive terminal
reindexdb -- reindex a PostgreSQL database
vacuumdb -- garbage-collect and analyze a PostgreSQL database
Los Angeles: POINT(-118.4079 33.9434)
Paris: POINT(2.3490 48.8533)
#距离
SELECT ST_Distance(
ST_GeometryFromText('POINT(-118.4079 33.9434)', 4326), -- Los Angeles (LAX)
ST_GeometryFromText('POINT(2.5559 49.0083)', 4326) -- Paris (CDG)
);
121.898285970107度
#距离米
SELECT ST_Distance(
ST_GeographyFromText('POINT(-118.4079 33.9434)'), -- Los Angeles (LAX)
ST_GeographyFromText('POINT(2.5559 49.0083)') -- Paris (CDG)
);
9124665.26917268米
Geometry :几何坐标
1234567891011121314151617181920
ST_AsText
#点
ST_X ST_Y
#线
ST_Length 线长度
ST_StartPoint 起点
ST_EndPoint 终点
ST_NPoints 点数
#多边形
ST_Area(geometry)面积
ST_NRings(geometry)环个数
ST_ExteriorRing(geometry) 外接环
ST_InteriorRingN(geometry,n) 内接环
ST_Perimeter(geometry) 所有环的长度
#集合
MultiPoint, a collection of points
MultiLineString, a collection of linestrings
MultiPolygon, a collection of polygons
GeometryCollection, a heterogeneous collection of any geometry (including other collections)
SELECT id, name, city_code, st_astext(gpoint) FROM poi
WHERE ST_DWithin(GeomFromText('POINT(117.28243891065 39.096105298613)',4326), gpoint, 1000)
ORDER BY ST_Distance(GeomFromText('POINT(117.28243891065 39.096105298613)',4326), gpoint)
LIMIT 10;