PostgreSQL

来自开放百科 - 灰狐
2008年2月26日 (二) 02:08Allen (讨论 | 贡献)的版本

跳转到: 导航, 搜索
Postgresql.jpeg

Postgresql.jpg PostgreSQL: The world's most advanced open source database

目录

简介

PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统。

PostgreSQL 的特性覆盖了 SQL-2/SQL-92 和 SQL-3/SQL-99,首先,它包括了可以说是目前世界上最丰富的数据类型的支持,其中有些数据类型可以说连商业数据库都不具备, 比如 IP 类型和几何类型等;其次,PostgreSQL 是全功能的自由软件数据库,很长时间以来,PostgreSQL 是唯一支持事务、子查询、多版本并行控制系统、数据完整性检查等特性的唯一的一种自由软件的数据库管理系统。直到最近才有 Inprise 的 InterBase 以及 SAP 等厂商将其原先专有软件开放为自由软件之后才打破了这个唯一。

PostgreSQL 提供了统一的客户端 C 接口。而不同的客户端接口都是源自这个 C 接口,比如 ODBC,JDBC,Python,Perl ,Tcl,C/C++,ESQL 等。

PostgreSQL 已经完全可以胜任任何中上规模范围内的应用范围的业务。目前有报道的生产数据库的大小已经有 TB 级的数据量,已经逼近 32 位计算的极限。 PostgreSQL是目前支持平台最多的数据库管理系统的一种, 所支持的平台多达十几种,包括不同的系统,不同的硬件体系。至今,它仍然保持着支持平台最多的数据库管理系统的称号。

PostgreSQL 的确还欠缺一些比较高端的数据库管理系统需要的特性,比如联机热备份,数据库集群,更优良的管理工具和更加自动化的系统优化功能 等提高数据库性能的机制等。

总而言之,PostgreSQL的特性已经完全可以满足绝大部分用户的需要,而且其质量和特性仍然在日新月异地进步着,所以, 我们有理由相信在不远的将来,PostgreSQL 肯定能够成为一种优秀的,自由的,商业数据库的替代产品。

最新版:8.2 http://blogs.huihoo.com/?p=307

安装指南

./configure
gmake // ln -s /usr/bin/make /usr/bin/gmake
su
gmake install
adduser postgres
mkdir /usr/local/pgsql/data
chown postgres /usr/local/pgsql/data
su - postgres // sudo su postgres 
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data 
/usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >logfile 2>&1 &
/usr/local/pgsql/bin/createdb test
/usr/local/pgsql/bin/psql test

stop postgresql

su postgres or sudo su postgres 
/usr/local/pgsql/bin/pg_ctl stop -D /usr/local/pgsql/data/

注意: postgres在默认是不接受tcp/ip连接的。有两种方式来控制它接受tcp/ip连接,一种是用启动参数 -i ,另一种方式是修改数据库目录里的文件:postgresql.conf中的参数,将tcpip_socket = false改为true port = 5432 前面的注释符号去掉。表示接受tcp/ip在5432的连接。(The tcpip_socket variable from postgres 7.4 is not recognized with postgres 8.1)version 8.0, this is controlled using the "listen_addresses"parameter in the postgresql.conf.

listen_addresses = 'localhost'
listen_addresses = '*' // * = all
listen_addresses = '192.168.1.2','192.168.1.3'

vi pg_hba.conf

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1 255.255.255.255 trust
host all postgres 127.0.0.1 255.255.255.255 password
or 
host all all 127.0.0.1/32 md5
host all all 192.168.0.0/24 md5
host all all 172.18.0.0/16 md5

修改后重启:

/etc/init.d/postgresql-7.4 restart

创建一测试用户: test

# edit file /etc/postgresql/pg_hba.conf, and lines at file top:
local   test       test                                          md5
host    test       test     127.0.0.1         255.255.255.255    md5
# su - postgres
$ createuser -ADPE test
$ createdb -O test test
$ createlang plpgsql test
$ exit
# psql -h 127.0.0.1 -U test test
 

Examples

http://pgfoundry.org/projects/dbsamples/ - A collection of sample databases for PostgreSQL.

请下载 dellstore2 ,大家可用这些数据做实验 :)

如 Dell Store2 database http://linux.dell.com/dvdstore/ , dbsamples 对 dvdstore database 做了 PosgreSQl port

Dell Store2 Physical Data Diagram

createdb -E LATIN1 dellstore2 -U postgres
createlang plpgsql dellstore2 -U postgres
psql -f dellstore.sql dellstore2 -U postgres

对其它数据库,如 mysql 请下载 ds2.tar.gz 和 ds2_mysql.tar.gz, 并解压到同一目录下, 并事先创建 DS2 数据库和 web/web 的用户信息

命令

PostgreSQL 体系结构和进程概况
  • createdb—Creates a new database in postgreSQL. A database name is required when using this command.
  • createuser—Creates a postgreSQL user. A user name is required when using this command.
  • dropdb—Removes a database from postgreSQL. A database name is required when using this command.
  • dropuser—Removes a postgreSQL user. A user name is required when using this command.
  • initdb—Creates a new postgreSQL database installation. This command is used only once when the database is installed. It creates the base directory. This command cannot be used again.
  • psql—SQL interface to the postgreSQL databases. A database name is required when using this command.
  • vacuumdb—Cleans and analyzes a postgreSQL database.

常用用法

$ createdb mydb
$ dropdb mydb
$ cd src/tutorial
$ export PATH=$PATH:/usr/local/pgsql/bin
$ psql -s mydb
alter user postgres with password 'postgres';
mydb=> \dt 命令是显示表
mydb=> \di 显示索引
mydb=> \ds 显示序列
mydb=> \dv 显示视图
mydb=> \dp 显示权限
mydb=> \dS 显示系统表
mydb=> \dl 显示 lobjects
mydb=> \d tablename 显示表的结构/索引/序列
mydb=> \d indexname 显示索引的详细信息
mydb=> \i basics.sql or basics.source 导入表
mydb=> SELECT version();
mydb=> show all; // 显示运行时参数的数值
pg_dump -x databasename > outfile
psql -e database < outfile

CREATE TABLE weather (
   city            varchar(80),
   temp_lo         int,           -- 最低气温
   temp_hi         int,           -- 最高气温
   prcp            real,          -- 降水量
   date            date
);
DROP TABLE tablename;

postgres@debian:~$ psql -l
       List of databases
   Name    |  Owner   | Encoding
-----------+----------+----------
 template0 | postgres | EUC_CN
 template1 | postgres | EUC_CN
 test      | postgres | EUC_CN
(3 rows)
postgres@debian:~$ psql template1
template1=# select * from pg_tables;
template1=# create user huihoo password 'huihoo' createdb;
mydb=# select md5('abc');
mydb=# select btrim('xyxtrimyyx','xy');
mydb=# select ltrim('zzzytrim','xyz');

http://docs.huihoo.com/postgresql/postgresql-doc-7.4-zh_CN/functions-string.html

PostgreSQL Limits

Limit Value

  • Maximum Database Size Unlimited
  • Maximum Table Size 32 TB
  • Maximum Row Size 1.6 TB
  • Maximum Field Size 1 GB
  • Maximum Rows per Table Unlimited
  • Maximum Columns per Table 250 - 1600 depending on column types
  • Maximum Indexes per Table Unlimited

PostgreSQL derived databases

  • Amalgamated Insight ... proprietary ... Fork of TelegraphCQ
  • Bizgres ... BSD ... PostgreSQL + BI features
  • Bizgres MPP ... proprietary ... PostgreSQL + BI features
  • EnterpriseDB ... proprietary ... PostgreSQL + Oracle compatibility
  • ExtenDB ... proprietary ... PostgreSQL + BI Features
  • Great Bridge PostgreSQL ... BSD ... PostgreSQL re-distribution
  • Mammoth ... BSD ... PostgreSQL + contrib modules
  • Netezza ... proprietary ... Appliance based on PostgreSQL SQL engine
  • NuSphere UltraSQL ... proprietary ... Native Win32 port of PostgreSQL
  • parACCEL ... proprietary ... PostgreSQL + BI features
  • Pervasive PostgreSQL ... BSD ... PostgreSQL re-distribution
  • PowerGres ... proprietary ... Native Win32 port of PostgreSQL
  • PowerGres Plus ... proprietary ... PostgreSQL + custom storage engine
  • PostgreSQL for Solaris 10 ... BSD ... PostgreSQL re-distribution
  • Red Hat Database ... BSD ... PostgreSQL re-distribution
  • TelegraphCQ ... BSD ... Data Stream oriented fork of PostgresSQL

from http://www.postgresql.org/docs/techdocs.62


Graphical Clients

Replication

PostGIS

PostGIS在对象关系型数据库PostgreSQL上增加了存储管理空间数据的能力,相当于Oracle的spatial部分。PostGIS最大的特点是符合并且实现了OpenGIS的一些规范,是最著名的开源GIS数据库。

Cluster/HPC

Storage

Powered by best-in-class open source software including the Solaris Operating System (OS), PostgreSQL, and ZFS.

http://www.greenplum.com/solutions/sun_Appliance.php

Java

http://jdbc.postgresql.org/

wget http://jdbc.postgresql.org/download/pg74.216.jdbc3.jar // JDK 1.4, 1.5
wget http://jdbc.postgresql.org/download/postgresql-8.1-409.jdbc3.jar // JDK 1.4 1.5

相关项目

More Projects:

商业支持

成功应用

在线文档

http://docs.huihoo.com/postgresql/

相关链接

Buttons

Postgresql 80x15.png

精彩图集

分享您的观点
个人工具
名字空间

变换
操作
导航
工具箱