PostgreSQL数据库开发使用手册-22
- 格式:doc
- 大小:4.63 MB
- 文档页数:2
postgresql教程PostgreSQL是一个开源的关系数据库管理系统(RDBMS),它的目标是成为最先进的开源数据库,并支持许多标准SQL 功能以及一些不同的高级功能。
本教程将向您介绍PostgreSQL的基本概念和用法。
我们将从安装和设置开始,然后逐步深入了解表、视图、索引、事务和查询等主题。
第一部分:安装和设置在本部分中,我们将向您介绍如何下载、安装和设置PostgreSQL数据库。
我们还将介绍一些基本概念和术语,如数据库、表和列。
1. 安装PostgreSQL:在本节中,我们将向您展示如何从官方网站或其他来源下载并安装PostgreSQL。
2. 设置数据库连接:在本节中,我们将介绍如何设置和配置数据库连接,包括创建用户、设置密码和分配权限等内容。
3. 创建数据库和表:在本节中,我们将介绍如何创建数据库和表,并向您展示一些常用的数据类型和约束。
第二部分:表和视图在本部分中,我们将更详细地介绍表格和视图的概念,并向您展示如何使用它们来存储和查询数据。
1. 创建和修改表格:在本节中,我们将介绍如何创建新的表格,并向您展示如何修改和删除现有的表格。
2. 数据类型和约束:在本节中,我们将深入了解不同的数据类型和约束,并向您展示如何使用它们来保证数据的完整性和一致性。
3. 视图和触发器:在本节中,我们将向您展示如何创建和使用视图和触发器,以及如何利用它们来简化复杂的查询和操作。
第三部分:索引和查询优化在本部分中,我们将介绍索引和查询优化的概念,并向您展示如何使用索引来提高查询性能。
1. 索引的概念和类型:在本节中,我们将介绍不同类型的索引,如B-tree、哈希和GiST索引,并向您展示如何创建和使用它们。
2. 查询优化和性能调优:在本节中,我们将介绍一些常见的查询优化技术,如查询计划、索引优化和统计信息收集等。
3. 复杂查询和连接:在本节中,我们将向您展示如何编写复杂的查询,包括多表连接、子查询和聚合等。
在PostgreSQL执行SQL询和管理数据库在PostgreSQL 中执行SQL 查询和管理数据库,可以按照以下步骤进行:1.连接到数据库:首先,您需要使用一个支持PostgreSQL 的客户端连接到数据库。
如果您正在本地计算机上运行PostgreSQL,可以使用psql命令行工具。
在命令行中输入以下命令:bash复制代码psql -U username -d database_name其中,username是您的PostgreSQL 用户名,database_name是您要连接的数据库名称。
输入密码后,您将连接到数据库。
2. 执行SQL 查询:一旦连接到数据库,您可以开始执行SQL 查询。
在命令行中输入SQL 查询,然后按回车键。
例如,要选择所有记录,可以输入:sql复制代码SELECT * FROM table_name;其中,table_name是要查询的表的名称。
3. 管理数据库:除了执行查询外,您还可以使用SQL 命令来管理数据库。
以下是一些常用的数据库管理命令:•创建表:使用CREATE TABLE命令创建表。
例如:sql复制代码CREATE TABLE table_name (column1 datatype, column2datatype, ...);•插入数据:使用INSERT INTO命令将数据插入表中。
例如:sql复制代码INSERT INTO table_name (column1, column2, ...) VALUES(value1, value2, ...);•更新数据:使用UPDATE和SET命令更新表中的数据。
例如:sql复制代码UPDATE table_name SET column1 = value1, column2 = value2WHERE condition;•删除数据:使用DELETE FROM命令从表中删除数据。
例如:sql复制代码DELETE FROM table_name WHERE condition;•查询数据:使用SELECT命令查询表中的数据。
PostgreSQL使用文档
安装完成后,大家最关注的就是使用了,那我们就开看看PostgreSQL这个数据库是怎么样来实现,备份和恢复功能的.
打开我们的PostgreSQL,你可以在开始---程序中轻易的找到
登陆我们的服务器
双击localhost(localhost:5432)
就会出现下图的对话框,输入我们的密码.
我们数据库的信息就展现在我们眼前了
数据库eg中有表asd
然后我们右键备份
选择要保存的地址,然后保存
然后我们进行模拟的破坏,把表删除.
把它右键一处相关依赖,这样他在触发器,序列等便没有了关联
这样表中就没有数据了.
在数据库eg上右键恢复
选择我们刚才保存的文件,单击确定
. 会弹出一个警告,不过不用管他,点击取消
再单击我们的数据表表的时候,我们的asd表就回来了
表和数据库的备份和删除有一点不同.
数据库的恢复是在删除数据库后重新创建一个新的,没有其他东西的数据库后,再恢复.
恢复后就会出现,asd表
备份和恢复时的数据库名称不一定要一样,这样我们就得出可以恢复任何一个数据,跟数据的复制一样简便.。
PostgreSQL数据库学习手册之libpq - C 库——介绍(转)[@more@] Chapter 1. libpq - C 库Table of Contents1。
1. 介绍1。
2. 数据库联接函数1.3. 命令执行函数1.3。
1. 主过程1。
3.2. 为包含在 SQL 查询中逃逸字串1.3.3. 逃逸包含在 SQL 查询中的二进制字串1.3.4。
检索 SELECT 的结果信息1.3.5. 检索 SELECT 结果数值1。
3.6. 检索非—SELECT 结果信息1.4。
异步查询处理1.5. 捷径接口1。
6。
异步通知1。
7。
与 COPY 命令相关的函数1.8。
libpq 跟踪函数1。
9。
libpq 控制函数1.10. 环境变量1。
11. 文件1。
12. 线程特性1。
13。
制作 Libpq 程序1。
14。
例子程序1.1. 介绍libpq 是 PostgreSQL的 C 应用程序员的接口. libpq 是一套允许客户程序向 PostgreSQL 后端服务进程发送查询并且获得查询返回的库过程. libpq 同时也是其他几个 PostgreSQL 应用接口下面的引擎,包括 libpq++ (C++), libpgtcl(Tcl),Perl,和 ecpg.所以如果你使用这些软件包, libpq某些方面的特性会对你非常重要.本节末尾有三个小程序显示如何利用 libpq书写程序.在下面目录里面有几个完整的 libpq 应用的例子:src/test/examplessrc/bin/psql使用 libpq 的前端程序必须包括头文件 libpq-fe。
h 并且必须与 libpq 库链接.PostgreSQL数据库学习手册之libpq—C库——-数据库联接函数(转)[@more@]1。
2。
数据库联接函数下面的过程处理与 PostgreSQL 后端服务器联接的事情.一个应用程序一次可以与多个后端建立联接. (这么做的原因之一是访问多于一个数据库.) 每个连接都是用一个从PQconnectdb()或PQsetdbLogin() 获得的PGconn对象表示.注意,这些函数总是返回一个非空的对象指针, 除非存储器少得连个PGconn对象都分配不出来.在把查询发送给联接对象之前,可以调用PQstatus 函数来检查一下联接是否成功.*PQconnectdb 与后端数据库服务器建立一个新的联接.PGconn *PQconnectdb(const char *conninfo)这个过程用从一个字符串 conninfo 来的参数与数据库打开一个新的联接.与下面的PQsetdbLogin()不同的是, 我们可以不必更换函数签名(名字)就可以扩展参数集, 所以我们建议应用程序中使用这个函数或者是它的非阻塞的相似函数 PQconnectStart 和 PQconnectPoll.传入的参数可以为空,表明使用所有缺省的参数,或者可以包含一个或更多个用空白间隔的参数设置.每个参数以关键字 = 数值的形式设置. (要写一个空值或者一个包含空白的值,你可以用一对单引号包围它们,例如, keyword = 'a value' .数值内部的单引号和反斜扛必须用一个反斜扛逃逸,也就是说,'或.) 等号周围的空白是可选的.目前可识别的参数键字是:host要联接的主机(host ).如果主机名以斜扛开头,则它声明使用 Unix 域套接字通讯而不是 TCP/IP 通讯; 该值就是套接字文件所存储的目录.缺省时是与位于 /tmp 里面的 Unix—域套接字联接.hostaddr与之联接的主机的 IP 地址。
PostgreSQL学习⼿册(⽬录)事实上之前有很长⼀段时间都在纠结是否有必要好好学习它,但是始终都没有⼀个很好的理由说服⾃⼰。
甚⾄是直到这个项⽬最终决定选⽤PostgreSQL时,我都没有真正意识到学习它的价值,当时只是想反正和其它数据库差不多,能⽤就⾏了。
然⽽有⼀天晚上,⾃⼰也不知道为什么,躺在床上开始回想整个项⽬的实施过程,想着想着就想到了数据库选型这⼀问题上了。
事情是这样的,最初客户将他们的产品⽬标定位为主打中型规模,同时也要在⼀定程度上⽀持⼩型规模。
鉴于此,我们为他们提供的⽅案是中型规模的数据库选⽤Oracle,⼩型规模的选定MySQL,在经过多轮商谈之后这个⽅案通过了。
然⽽随着项⽬的深⼊,客户突然有⼀天提出,由于成本和市场推⼴等问题,该产品的数据库部分需要进⾏⼀定的调整,调整的结果是中型规模可以同时⽀持Oracle和MySQL,⽽⼩型规模则要同时⽀持MySQL和PostgreSQL,原因⾮常简单,PostgreSQL是纯免费的数据库产品。
听到这个消息之后,我当时就⾮常恼⽕,因为当初为了保证运⾏时效率(国标),我们的数据库访问层是完全基于数据库供应商提供的原始C接⼝开发的,甚⾄都没有考虑ODBC提供的原始C接⼝,以防在转换中失去效率,或是ODBC本⾝为了强调通⽤性⽽不得不牺牲某些数据库的优化特征,如批量插⼊、批量读取等。
最后的结果显⽽易见,客户就是上帝,上帝的意见就是真理,这样我们就不得不基于现有的访问层接⼝⼜重新开发了⼀套⽀持PostgreSQL原⽣C接⼝的驱动。
然⽽随着对PostgreSQL的不断学习,对它的了解也在逐步加深,后来发现它的功能还是⾮常强⼤的,特别是对GIS空间数据的⽀持就更加的吸引我了。
于是就在脑⼦⾥为MySQL和PostgreSQL做了⼀个简单的对⽐和分析,最后得出⼀个结论,相⽐MySQL,PostgreSQL并没有什么刚性的缺点,但是它的纯免费特征确实是MySQL⽆法⽐拟的。
云数据库 PostgreSQL操作指南产品⽂档【版权声明】©2013-2023 腾讯云版权所有本⽂档著作权归腾讯云单独所有,未经腾讯云事先书⾯许可,任何主体不得以任何形式复制、修改、抄袭、传播全部或部分本⽂档内容。
【商标声明】及其它腾讯云服务相关的商标均为腾讯云计算(北京)有限责任公司及其关联公司所有。
本⽂档涉及的第三⽅主体的商标,依法由权利⼈所有。
【服务声明】本⽂档意在向客户介绍腾讯云全部或部分产品、服务的当时的整体概况,部分产品、服务的内容可能有所调整。
您所购买的腾讯云产品、服务的种类、服务标准等应由您与腾讯云之间的商业合同约定,除⾮双⽅另有约定,否则,腾讯云对本⽂档内容不做任何明⽰或模式的承诺或保证。
⽂档⽬录操作指南实例管理实例⽣命周期设置实例维护时间调整实例配置变更可⽤区设置销毁实例恢复实例下线实例重启实例升级实例升级内核⼩版本只读实例只读实例概述管理只读实例 RO 组剔除策略和负载均衡帐号管理数据库权限概述⽤户与权限操作数据库优化慢查询分析错误⽇志参数管理设置实例参数参数值限制备份与恢复备份数据下载备份克隆实例⾃动备份设置在云服务器上恢复 PostgreSQL 数据删除备份查看备份空间设置备份下载规则插件管理插件概述⽀持插件⽀持插件版本概览PostgreSQL 9.3 ⽀持插件PostgreSQL 9.5 ⽀持插件PostgreSQL 10 ⽀持插件PostgreSQL 11 ⽀持插件PostgreSQL 12 ⽀持插件PostgreSQL 13 ⽀持插件PostgreSQL 14 ⽀持插件pgAgent 插件跨库访问位图计算 pg_roaringbitmap 插件定时任务 pg_cron 插件⽹络管理⽹络管理概述修改⽹络开启外⽹地址访问管理访问管理概述授权策略语法可授权的资源类型控制台⽰例数据加密透明数据加密概述开启透明数据加密安全组管理安全组关联实例⾄安全组监控与告警监控功能告警功能标签标签概述编辑标签操作指南实例管理实例⽣命周期最近更新时间:2021-07-06 10:55:18云数据库 PostgreSQL 实例有诸多状态,不同状态下实例可执⾏的操作不同。
PostgreSQL使用方法本文将介绍PostgreSQL的一些基本使用方法,包括安装、配置、创建数据库、表和用户,以及常用的查询语句和操作。
1. 安装和配置PostgreSQL1.2 配置PostgreSQL:在安装过程中,您将被要求设置一个超级用户密码。
此密码将用于登录PostgreSQL服务器。
2.创建数据库、表和用户2.1创建数据库:可以使用以下命令创建一个新的数据库。
CREATE DATABASE database_name;2.2连接到数据库:可以使用以下命令连接到一个数据库。
\c database_name;2.3创建表:在连接到数据库后,可以使用以下命令创建一个新的表。
您可以指定表的列及其数据类型。
CREATE TABLE table_namecolumn1 datatype1,column2 datatype2,....2.4创建用户:可以使用以下命令创建一个新的用户。
CREATE USER username WITH PASSWORD 'password';3.数据库查询和操作3.1插入数据:可以使用以下命令将数据插入到表中。
INSERT INTO table_name (column1, column2, ...)VALUES (value1, value2, ...);3.2更新数据:可以使用以下命令更新表中的数据。
UPDATE table_nameSET column1 = value1, column2 = value2, ...WHERE condition;3.3删除数据:可以使用以下命令从表中删除数据。
DELETE FROM table_name WHERE condition;3.4查询数据:可以使用以下命令从表中检索数据。
SELECT column1, column2, ...FROM table_nameWHERE condition;3.5排序和过滤:您可以使用ORDERBY子句对结果进行排序,并使用WHERE子句过滤结果。
postgresql中文手册PostgreSQL中文手册概述PostgreSQL是一个功能强大的开源关系型数据库管理系统,它提供了多种高级特性,可以满足各种规模的应用需求。
本文档将为您介绍PostgreSQL的基本概念、使用方法以及高级特性。
第一章:入门指南1.1 安装PostgreSQL本节将指导您如何下载、安装和配置PostgreSQL。
您可以根据不同的操作系统选择适合的安装方法,并了解基本的配置选项。
1.2 数据库连接学习如何连接到PostgreSQL数据库,并使用命令行工具或者图形化界面进行操作。
您将了解如何创建、删除和管理数据库。
1.3 SQL语法PostgreSQL支持标准的SQL语法,并且还提供了许多扩展功能。
本节将介绍常用的SQL语句,包括表的创建、数据的插入、更新和删除,以及查询语句的使用。
第二章:高级特性2.1 事务管理了解PostgreSQL的事务管理功能,包括事务的启动、提交和回滚,并学习如何处理并发访问和锁定。
2.2 索引和性能优化学习如何创建索引以提高查询性能,并了解如何使用EXPLAIN命令来分析查询执行计划。
2.3 触发器和事件管理本节将介绍如何使用触发器来自动化处理某些数据库事件,并学习如何对触发器进行管理和监控。
2.4 备份和恢复学习如何创建数据库的备份,并了解如何恢复数据库的数据。
第三章:高级应用3.1 数据复制本节介绍PostgreSQL的数据复制功能,包括主从复制和逻辑复制。
您将学习如何配置复制服务器,并设置故障转移和负载均衡。
3.2 分区表了解如何使用分区表来提高查询性能和管理大型数据集。
3.3 全文搜索学习如何使用PostgreSQL的全文搜索功能,包括设置搜索引擎和执行高级搜索查询。
3.4 GIS支持了解如何使用PostGIS扩展来处理地理信息系统数据,并学习如何执行GIS查询和空间分析。
结论本文档提供了关于PostgreSQL的全面介绍,包括基本概念、使用方法和高级特性。
PostgreSQL学习⼿册(PLpgSQL过程语⾔)⼀、概述:PL/pgSQL函数在第⼀次被调⽤时,其函数内的源代码(⽂本)将被解析为⼆进制指令树,但是函数内的表达式和SQL命令只有在⾸次⽤到它们的时候,PL/pgSQL解释器才会为其创建⼀个准备好的执⾏规划,随后对该表达式或SQL命令的访问都将使⽤该规划。
如果在⼀个条件语句中,有部分SQL命令或表达式没有被⽤到,那么PL/pgSQL解释器在本次调⽤中将不会为其准备执⾏规划,这样的好处是可以有效地减少为PL/pgSQL函数⾥的语句⽣成分析和执⾏规划的总时间,然⽽缺点是某些表达式或SQL命令中的错误只有在其被执⾏到的时候才能发现。
由于PL/pgSQL在函数⾥为⼀个命令制定了执⾏计划,那么在本次会话中该计划将会被反复使⽤,这样做往往可以得到更好的性能,但是如果你动态修改了相关的数据库对象,那么就有可能产⽣问题,如:CREATE FUNCTION populate() RETURNS integer AS $$DECLARE-- 声明段BEGINPERFORM my_function();END;$$ LANGUAGE plpgsql;在调⽤以上函数时,PERFORM语句的执⾏计划将引⽤my_function对象的OID。
在此之后,如果你重建了my_function函数,那么populate函数将⽆法再找到原有my_function函数的OID。
要解决该问题,可以选择重建populate函数,或者重新登录建⽴新的会话,以使PostgreSQL重新编译该函数。
要想规避此类问题的发⽣,在重建my_function时可以使⽤CREATE OR REPLACE FUNCTION命令。
鉴于以上规则,在PL/pgSQL⾥直接出现的SQL命令必须在每次执⾏时均引⽤相同的表和字段,换句话说,不能将函数的参数⽤作SQL命令的表名或字段名。
如果想绕开该限制,可以考虑使⽤PL/pgSQL中的EXECUTE语句动态地构造命令,由此换来的代价是每次执⾏时都要构造⼀个新的命令计划。
postgre sql 使用PostgreSQL(常简称为Postgres)是一个强大且广受欢迎的关系型数据库管理系统(DBMS)。
它提供了许多高级功能和灵活性,且适用于各种应用程序,从小型项目到大型企业级系统。
本文将以PostgreSQL的使用为主题,带领你一步一步学习这个强大的数据库管理系统。
第一步:安装和设置PostgreSQL首先,你需要下载并安装PostgreSQL。
你可以从官方网站(第二步:连接到PostgreSQL数据库一旦安装和设置完成,你可以使用不同的客户端工具连接到PostgreSQL 数据库。
一种常见的工具是命令行界面(CLI)工具psql。
在终端中输入“psql”命令,然后提供PostgreSQL服务器的连接信息(主机名、用户名和密码)。
成功连接后,你将看到一个以“postgres=#”开头的提示符,表示你已经成功登录到数据库。
第三步:创建和管理数据库通过CLI或其他数据库管理工具连接到PostgreSQL后,你可以开始创建和管理数据库。
使用CREATE DATABASE语句可以创建一个新的数据库。
例如,运行以下命令来创建一个名为“mydatabase”的数据库:CREATE DATABASE mydatabase;你还可以使用DROP DATABASE语句来删除一个已经存在的数据库:DROP DATABASE mydatabase;要查看所有已存在的数据库,可以使用以下命令:\l该命令将显示所有数据库的列表,以及它们的所有者和大小等信息。
第四步:创建表和插入数据数据库的核心是表格,它们用于组织和存储数据。
要创建一个新的表格,你可以使用CREATE TABLE语句。
以下是一个例子,创建一个名为“employees”的表格:CREATE TABLE employees (id SERIAL PRIMARY KEY,name VARCHAR(100),age INT,salary DECIMAL(10,2));这个表格具有id、name、age和salary等列。
目录PostgreSQL学习手册(数据表) (4)一、表的定义: (4)PostgreSQL学习手册(模式Schema) (9)PostgreSQL学习手册(表的继承和分区) (10)一、表的继承: (10)PostgreSQL学习手册(常用数据类型) (16)一、数值类型: (16)六、数组: (22)PostgreSQL学习手册(函数和操作符<一>) (25)一、逻辑操作符: (25)四、字符串函数和操作符: (27)五、位串函数和操作符: (29)PostgreSQL学习手册(函数和操作符<二>) (30)六、模式匹配: (30)八、时间/日期函数和操作符: (33)PostgreSQL学习手册(函数和操作符<三>) (35)九、序列操作函数: (35)十二、系统信息函数: (38)PostgreSQL学习手册(索引) (42)一、索引的类型: (42)四、唯一索引: (43)PostgreSQL学习手册(事物隔离) (45)PostgreSQL学习手册(性能提升技巧) (46)一、使用EXPLAIN: (46)PostgreSQL学习手册(服务器配置) (50)一、服务器进程的启动和关闭: (50)PostgreSQL学习手册(角色和权限) (52)PostgreSQL学习手册(数据库管理) (54)一、概述: (54)PostgreSQL学习手册(数据库维护) (56)一、恢复磁盘空间: (56)二、更新规划器统计: (57)四、定期重建索引: (59)PostgreSQL学习手册(系统表) (61)一、pg_class: (61)三、pg_attrdef: (63)四、pg_authid: (64)五、pg_auth_members: (64)七、pg_tablespace: (65)十、pg_index: (67)PostgreSQL学习手册(系统视图) (68)一、pg_tables: (68)二、pg_indexes: (68)三、pg_views: (68)四、pg_user: (69)五、pg_roles: (69)六、pg_rules: (69)七、pg_settings: (70)PostgreSQL学习手册(客户端命令<一>) (70)零、口令文件: (70)PostgreSQL学习手册(客户端命令<二>) (75)七、pg_dump: (75)八、pg_restore: (77)PostgreSQL学习手册(SQL语言函数) (83)一、基本概念: (83)PostgreSQL学习手册(PL/pgSQL过程语言) (86)一、概述: (86)PostgreSQL学习手册(数据表)一、表的定义:对于任何一种关系型数据库而言,表都是数据存储的最核心、最基础的对象单元。
零、口令文件:在给出其它PostgreSQL客户端命令之前,我们需要先介绍一下PostgreSQL中的口令文件。
之所以在这里提前说明该文件,是因为我们在后面的示例代码中会大量应用该文件,从而保证我们的脚本能够自动化完成。
换句话说,如果在客户端命令执行时没有提供该文件,PostgreSQL的所有客户端命令均会被口令输入提示中断。
在当前用户的HOME目录下,我们需要手工创建文件名为 .pgpass的口令文件,这样就可以在我们连接PostgreSQL服务器时,客户端命令自动读取该文件已获得登录时所需要的口令信息。
该文件的格式如下:hostname:port:database:username:password以上数据是用冒号作为分隔符,总共分为五个字段,分别表示服务器主机名(IP)、服务器监听的端口号、登录访问的数据库名、登录用户名和密码,其中前四个字段都可以使用星号(*)来表示匹配任意值。
见如下示例:/> cat > .pgpass*:5432:postgres:postgres:123456CTRL+D#.pgpass文件的权限必须为0600,从而防止任何全局或者同组的用户访问,否则这个文件将被忽略。
/> chmod 0600 .pgpass在学习后面的客户端命令之前,我们需要根据自己的应用环境手工创建该文件,以便后面所有的示例代码都会用到该口令文件,这样它们就都可以以批处理的方式自动完成。
一、createdb:创建一个新的PostgreSQL数据库。
该命令的使用方式如下:createdb [option...] [dbname] [description]1. 命令行选项列表:选项说明-D(--tablespace=tablespace)指定数据库的缺省表空间。
-e(--echo)回显createdb生成的命令并且把它发送到服务器。
-E(--encoding=encoding)指定用于此数据库的字符编码方式。
PostgreSQL学习⼿册(常⽤数据类型)⼀、数值类型:下⾯是PostgreSQL所⽀持的数值类型的列表和简单说明:名字存储空间描述范围smallint 2 字节⼩范围整数-32768 到 +32767integer 4 字节常⽤的整数-2147483648 到 +2147483647bigint8 字节⼤范围的整数-9223372036854775808 到 9223372036854775807decimal变长⽤户声明精度,精确⽆限制numeric变长⽤户声明精度,精确⽆限制real 4 字节变精度,不精确 6 位⼗进制数字精度double8 字节变精度,不精确15 位⼗进制数字精度serial 4 字节⾃增整数 1 到 +2147483647bigserial8 字节⼤范围的⾃增整数 1 到 92233720368547758071. 整数类型:类型smallint、integer和bigint存储各种范围的全部是数字的数,也就是没有⼩数部分的数字。
试图存储超出范围以外的数值将导致⼀个错误。
常⽤的类型是integer,因为它提供了在范围、存储空间和性能之间的最佳平衡。
⼀般只有在磁盘空间紧张的时候才使⽤smallint。
⽽只有在integer的范围不够的时候才使⽤bigint,因为前者(integer)绝对快得多。
2. 任意精度数值:类型numeric可以存储最多1000位精度的数字并且准确地进⾏计算。
因此⾮常适合⽤于货币⾦额和其它要求计算准确的数量。
不过,numeric类型上的算术运算⽐整数类型或者浮点数类型要慢很多。
numeric字段的最⼤精度和最⼤⽐例都是可以配置的。
要声明⼀个类型为numeric的字段,你可以⽤下⾯的语法:NUMERIC(precision,scale)⽐如数字23.5141的精度为6,⽽刻度为4。
在⽬前的PostgreSQL版本中,decimal和numeric是等效的。
PostgreSQL 学习手册(数据库管理一、概述:数据库可以被看成是SQL 对象(数据库对象的命名集合,通常而言,每个数据库对象(表、函数等只属于一个数据库。
不过对于部分系统表而言,如pg_database,是属于整个集群的。
更准确地说,数据库是模式的集合,而模式包含表、函数等SQL 对象。
因此完整的对象层次应该是这样的:服务器、数据库、模式、表或其他类型的对象。
在与数据库服务器建立连接时,该连接只能与一个数据库形成关联,不允许在一个会话中进行多个数据库的访问。
如以postgres 用户登录,该用户可以访问的缺省数据库为postgres ,在登录后如果执行下面的SQL 语句将会收到PostgreSQL 给出的相关错误信息。
postgres=# SELECT * FROM MyTest."MyUser".testtables;ERROR: cross-database references are not implemented:"otherdb.otheruser.sometable" LINE 1: select * from otherdb.otheruser.sometable在PostgreSQL 中,数据库在物理上是相互隔离的,对它们的访问控制也是在会话层次上进行的。
然而模式只是逻辑上的对象管理结构,是否能访问某个模式的对象是由权限系统来控制的。
执行下面的基于系统表的查询语句可以列出现有的数据库集合。
SELECT datname FROM pg_database;注:psql 应用程序的\l元命令和-l 命令行选项也可以用来列出当前服务器中已有的数据库。
二、创建数据库:在PostgreSQL 服务器上执行下面的SQL 语句可以创建数据库。
CREATE DATABASE db_name;在数据库成功创建之后,当前登录角色将自动成为此新数据库的所有者。
About the T utorialPostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.PostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows.This tutorial will give you quick start with PostgreSQL and make you comfortable with PostgreSQL programming.AudienceThis tutorial has been prepared for the beginners to help them understand the basic to advanced concepts related to PostgreSQL Database.PrerequisitesBefore you start practicing with various types of examples given in this reference, I'm making an assumption that you are already aware about what is database, especially RDBMS and what is a computer programming language.Copyright & DisclaimerCopyright 2017 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************T able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents (ii)1.PostgreSQL – Overview (1)Brief History (1)Key Features of PostgreSQL (2)Procedural Languages Support (2)2.PostgreSQL – Environment Setup (3)Installing PostgreSQL on Linux/Unix (3)Installing PostgreSQL on Windows (4)Installing PostgreSQL on Mac (7)3.PostgreSQL – Syntax (11)The SQL Statement (11)PostgreSQL SQL commands (11)4.PostgreSQL – Data Type (35)Numeric Types (35)Monetary Types (36)Character Types (36)Binary Data Types (37)Date/Time Types (37)Boolean Type (37)Enumerated Type (38)Geometric Type (38)Network Address Type (38)Bit String Type (39)Text Search Type (39)UUID Type (39)XML Type (39)JSON Type (40)Array Type (40)Composite Types (41)Range Types (42)Object Identifier Types (43)Pseudo Types (43)5.PostgreSQL – CREATE Database (45)Using createdb Command (45)6.PostgreSQL – SELECT Database (48)Database SQL Prompt (48)OS Command Prompt (49)7.PostgreSQL – DROP Database (50)Using dropdb Command (51)8.PostgreSQL – CREATE Table (53)9.PostgreSQL – DROP Table (55)10.PostgreSQL – Schema (56)Syntax to Create Table in Schema (56)Syntax to Drop Schema (57)11.PostgreSQL – INSERT Query (58)12.PostgreSQL – SELECT Query (60)13.PostgreSQL – Operators (62)PostgreSQL Arithmetic Operators (62)PostgreSQL Comparison Operators (64)PostgreSQL Logical Operators (66)PostgreSQL Bit String Operators (69)14.PostgreSQL – Expressions (71)PostgreSQL – Boolean Expressions (71)PostgreSQL – Numeric Expression (72)PostgreSQL – Date Expressions (73)15.PostgreSQL – WHERE Clause (74)16.PostgreSQL – AND & OR Conjunctive Operators (79)The AND Operator (79)The OR Operator (80)17.PostgreSQL – UPDATE Query (82)18.PostgreSQL – DELETE Query (84)19.PostgreSQL – LIKE Clause (86)20.PostgreSQL – LIMIT Clause (89)21.PostgreSQL – ORDER BY Clause (91)22.PostgreSQL – GROUP BY (94)23.PostgreSQL – WITH Clause (97)Recursive WITH (97)24.PostgreSQL – HAVING Clause (101)25.PostgreSQL – DISTINCT Keyword (104)ADVANCED POSTGRESQL (107)26.PostgreSQL – CONSTRAINTS (108)NOT NULL Constraint (108)UNIQUE Constraint (109)PRIMARY KEY Constraint (109)FOREIGN KEY Constraint (110)CHECK Constraint (111)EXCLUSION Constraint (111)Dropping Constraints (112)27.PostgreSQL – JOINS (113)The CROSS JOIN (114)The INNER JOIN (115)The LEFT OUTER JOIN (116)The RIGHT OUTER JOIN (117)The FULL OUTER JOIN (117)28.PostgreSQL – UNIONS Clause (119)The UNION ALL Clause (121)29.PostgreSQL – NULL Values (123)30.PostgreSQL – ALIAS Syntax (126)31.PostgreSQL – TRIGGERS (129)Listing TRIGGERS (132)Dropping TRIGGERS (132)32.PostgreSQL – INDEXES (133)Index Types (133)The DROP INDEX Command (135)When Should Indexes be Avoided? (135)33.PostgreSQL – ALTER TABLE Command (136)34.PostgreSQL – TRUNCATE TABLE Command (139)35.PostgreSQL – VIEWS (140)Creating Views (140)Dropping Views (142)36.PostgreSQL – TRANSACTIONS (143)Transaction Control (143)The COMMIT Command (144)The ROLLBACK Command (144)37.PostgreSQL – LOCKS (146)DeadLocks (146)Advisory Locks (147)38.PostgreSQL – Sub Queries (148)Subqueries with the SELECT Statement (148)Subqueries with the INSERT Statement (149)Subqueries with the UPDATE Statement (150)Subqueries with the DELETE Statement (151)39.PostgreSQL – AUTO INCREMENT (153)40.PostgreSQL – PRIVILEGES (155)41.PostgreSQL – DATE/TIME Functions and Operators (158)42.PostgreSQL – Functions (166)43.PostgreSQL – Useful Functions (168)PostgreSQL – COUNT Function (168)PostgreSQL – MAX Function (169)PostgreSQL – MIN Function (171)PostgreSQL – AVG Function (172)PostgreSQL – SUM Function (173)PostgreSQL – Array Function (174)PostgreSQL – Numeric Function (175)PostgreSQL – STRING Function (185)POSTGRESQL INTERFACES (197)44.PostgreSQL – C/C++ Interface (198)Installation (198)C/C++ Interface APIs (199)Connecting To Database (200)Create a Table (201)INSERT Operation (202)SELECT Operation (204)UPDATE Operation (206)DELETE Operation (208)45.PostgreSQL – JAVA Interface (211)Installation (211)Connecting To Database (211)Create a Table (212)INSERT Operation (213)SELECT Operation (215)UPDATE Operation (217)DELETE Operation (219)46.PostgreSQL – PHP Interface (222)Installation (222)PHP Interface APIs (222)Connecting to Database (224)Create a Table (225)INSERT Operation (226)SELECT Operation (227)UPDATE Operation (228)DELETE Operation (230)47.PostgreSQL – Perl Interface (233)Installation (233)DBI Interface APIs (234)Connecting to Database (235)Create a Table (235)INSERT Operation (236)SELECT Operation (237)UPDATE Operation (239)DELETE Operation (240)48.PostgreSQL – Python Interface (243)Installation (243)Python psycopg2 module APIs (243)Connecting to Database (245)Create a Table (245)INSERT Operation (246)SELECT Operation (247)UPDATE Operation (248)DELETE Operation (249)PostgreSQL 7PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development phase and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.This tutorial will give you a quick start with PostgreSQL and make you comfortable with PostgreSQL programming.What is PostgreSQL?PostgreSQL (pronounced as post-gress-Q-L ) is an open source relational database management system (DBMS) developed by a worldwide team of volunteers. PostgreSQL is not controlled by any corporation or other private entity and the source code is available free of charge.A Brief History of PostgreSQLPostgreSQL, originally called Postgres, was created at UCB by a computer science professor named Michael Stonebraker. Stonebraker started Postgres in 1986 as a follow-up project to its predecessor, Ingres, now owned by Computer Associates.1. 1977-1985: A project called INGRES was developed.∙Proof-of-concept for relational databases ∙Established the company Ingres in 1980 ∙ Bought by Computer Associates in 19942. 1986-1994: POSTGRES∙ Development of the concepts in INGRES with a focus on object orientation and the query language - Quel∙The code base of INGRES was not used as a basis for POSTGRES ∙ Commercialized as Illustra (bought by Informix, bought by IBM)3. 1994-1995: Postgres95∙Support for SQL was added in 1994 ∙Released as Postgres95 in 1995 ∙Re-released as PostgreSQL 6.0 in 1996 ∙ Establishment of the PostgreSQL Global Development Team1.PostgreSQLKey Features of PostgreSQLPostgreSQL runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. It supports text, images, sounds, and video, and includes programming interfaces for C / C++, Java, Perl, Python, Ruby, Tcl and Open Database Connectivity (ODBC).PostgreSQL supports a large part of the SQL standard and offers many modern features including the following:∙Complex SQL queries∙SQL Sub-selects∙Foreign keys∙Trigger∙Views∙Transactions∙Multiversion concurrency control (MVCC)∙Streaming Replication (as of 9.0)∙Hot Standby (as of 9.0)You can check official documentation of PostgreSQL to understand the above-mentioned features. PostgreSQL can be extended by the user in many ways. For example by adding new: ∙Data types∙Functions∙Operators∙Aggregate functions∙Index methodsProcedural Languages SupportPostgreSQL supports four standard procedural languages, which allows the users to write their own code in any of the languages and it can be executed by PostgreSQL database server. These procedural languages are - PL/pgSQL, PL/Tcl, PL/Perl and PL/Python. Besides, other non-standard procedural languages like PL/PHP, PL/V8, PL/Ruby, PL/Java, etc., are also supported.8PostgreSQL 9 To start understanding the PostgreSQL basics, first let us install the PostgreSQL. This chapter explains about installing the PostgreSQL on Linux, Windows and Mac OS platforms. Installing PostgreSQL on Linux/UnixFollow the given steps to install PostgreSQL on your Linux machine. Make sure you are logged in as root before you proceed for the installation.∙ Pick the version number of PostgreSQL you want and, as exactly as possible, the platform you want from EnterpriseDB∙I downloaded postgresql-9.2.4-1-linux-x64.run for my 64-bit CentOS-6 machine. Now, let us execute it as follows:∙ Once you launch the installer, it asks you a few basic questions like location of the installation, password of the user who will use database, port number, etc. So keep all of them at their default values except password, which you can provide password as per your choice. It will install PostgreSQL at your Linux machine and will display the following message:∙ Follow the following post-installation steps to create your database:2.10∙You can start/restart postgres server in case it is not running, using thefollowing command:∙If your installation was correct, you will have PotsgreSQL prompt test=# as shown above.Installing PostgreSQL on WindowsFollow the given steps to install PostgreSQL on your Windows machine. Make sure you have turned Third Party Antivirus off while installing.∙Pick the version number of PostgreSQL you want and, as exactly as possible, the platform you want from EnterpriseDB∙I downloaded postgresql-9.2.4-1-windows.exe for my Windows PC running in 32-bit mode, so let us run postgresql-9.2.4-1-windows.exe as administrator to install PostgreSQL. Select the location where you want to install it. By default, it is installed within Program Files folder.11The next step of the installation process would be to select the directory where your data would be stored. By default, it is stored under the "data" directory.12Next, the setup asks for password, so you can use your favorite password.13The next step; keep the port as default.14∙ In the next step, when asked for "Locale", I selected "English, United States".∙It takes a while to install PostgreSQL on your system. On completion of the installation process, you will get the following screen. Uncheck the checkbox and click the Finish button.After the installation process is completed, you can access pgAdmin III, StackBuilder and PostgreSQL shell from your Program Menu under PostgreSQL 9.2.Installing PostgreSQL on MacFollow the given steps to install PostgreSQL on your Mac machine. Make sure you are logged in as administrator before you proceed for the installation.∙∙I downloaded postgresql-9.2.4-1-osx.dmg for my Mac OS running with OS X version 10.8.3. Now, let us open the dmg image in finder and just double click it, which will give you PostgreSQL installer in the following window:15∙Next, click the postgres-9.2.4-1-osx icon, which will give a warning message. Accept the warning and proceed for further installation. It will ask for the administrator password as seen in the following window:∙Enter the password, proceed for the installation, and after this step, restart your Mac machine. If you do not see the following window, start your installation once again.16Once you launch the installer, it asks you a few basic questions like location of the installation, password of the user who will use database, port number etc. Therefore, keep all of them at their default values except the password, which you can provide as per your choice. It will install PostgreSQL in your Mac machine in the Application folder which you can check:17Now, you can launch any of the program to start with. Let us start with SQL Shell.When you launch SQL Shell, just use all the default values it displays except, enter your password, which you had selected at the time of installation. If everything goes fine, then you will be inside postgres database and a postgress#prompt will be displayed as shown below:18Congratulations Now you have your environment ready to start with PostgreSQL database programming.19PostgreSQL20This chapter provides a list of the PostgreSQL SQL commands, followed by the precise syntax rules for each of these commands. This set of commands is taken from the psql command-line tool. Now that you have Postgres installed, open the psql as: Program Files > PostgreSQL 9.2 > SQL Shell(psql).Using psql, you can generate a complete list of commands by using the \help command. For the syntax of a specific command, use the following command: The SQL StatementAn SQL statement is comprised of tokens where each token can represent either a keyword, identifier, quoted identifier, constant, or special character symbol. The table given below uses a simple SELECT statement to illustrate a basic, but complete, SQL statement and its components.PostgreSQL SQL commandsABORTAbort the current transaction. ALTER AGGREGATEChange the definition of an aggregate function. 3.ALTER CONVERSIONChange the definition of a conversion.ALTER DATABASEChange a database specific parameter.ALTER DOMAINChange the definition of a domain specific parameter.ALTER FUNCTIONChange the definition of a function.ALTER GROUPChange a user group.21ALTER INDEXChange the definition of an index.ALTER LANGUAGEChange the definition of a procedural language.ALTER OPERATORChange the definition of an operator.ALTER OPERATOR CLASSChange the definition of an operator class.ALTER SCHEMAChange the definition of a schema.ALTER SEQUENCEChange the definition of a sequence generator.ALTER TABLEChange the definition of a table.Where action is one of the following lines:ALTER TABLESPACE23Change the definition of a tablespace.ALTER TRIGGERChange the definition of a trigger.ALTER TYPEChange the definition of a type.ALTER USERChange a database user account.Where option can be:ANALYZECollect statistics about a database.BEGINStart a transaction block.24Where transaction_mode is one of:CHECKPOINTForce a transaction log checkpoint.CLOSEClose a cursor.CLUSTERCluster a table according to an index.COMMENTDefine or change the comment of an object.25COMMITCommit the current transaction.COPYCopy data between a file and a table.CREATE AGGREGATEDefine a new aggregate function.CREATE CASTDefine a new cast.27CREATE CONSTRAINT TRIGGERDefine a new constraint trigger.CREATE CONVERSIONDefine a new conversion.CREATE DATABASECreate a new database.CREATE DOMAINDefine a new domain.28Where constraint is:CREATE FUNCTIONDefine a new function.CREATE GROUPDefine a new user group.CREATE INDEXDefine a new index.29CREATE LANGUAGEDefine a new procedural language.CREATE OPERATORDefine a new operator.CREATE OPERATOR CLASSDefine a new operator class.30CREATE RULEDefine a new rewrite rule.CREATE SCHEMADefine a new schema.CREATE SEQUENCEDefine a new sequence generator.Define a new table.31Where column_constraint is:And table_constraint is:32End of ebook previewIf you liked what you saw…Buy it from our store @ https://33。
PostgreSQL数据库开发使用手册
根据日常开发的操作和使用,现对postgreSQL从三大部分进行描述说明,整体上覆盖了数据开发的各个方面。
第一部分postgreSQL数据库的安装配置管理
内容简介:
描述PostgreSQL数据库如何进行安装,以及安装后进行配置时注意的问题,简单介绍一下PostgreSQL命令行和工具的使用。
第二部分postgreSQL数据库的基本操作
内容简介:
介绍PostgreSQL数据库开发使用的基本操作,如select、update、insert、delete与create等,重点分析它们与其他数据库的区别,以及开发中需要特别注意的地方。
最后描述高级开发使用的视图、序列、触发器和存储过程。
第三部分postgreSQL数据库编程
内容简介:
详细描述java客户端程序与后端接口连接操作,以及与hibernate框架的整合。
重点介绍oracle 数据库如何移植到PostgreSQL数据库。
目录
第一部分PostgreSQL安装配置
第1章windows系统下,安装配置
1.1 安装配置
1、安装前的准备
软件版本:postgresql-9.0.2-1-windows.exe
2、安装。