https://wiki.postgresql.org/api.php?action=feedcontributions&user=Horen&feedformat=atomPostgreSQL wiki - User contributions [en]2024-03-28T19:29:51ZUser contributionsMediaWiki 1.35.13https://wiki.postgresql.org/index.php?title=9.1%E6%96%87%E6%A1%A3%E7%BF%BB%E8%AF%91%E9%A1%B9%E7%9B%AE&diff=208219.1文档翻译项目2013-09-20T07:48:05Z<p>Horen: </p>
<hr />
<div>参考文档<br />
*[http://www.pgsqldb.org/mwiki/index.php/PostgreSQL_8.3_%E6%96%87%E6%A1%A3 8.3 wiki版本]<br />
*[http://www.pgsqldb.org/pgsqldoc-8.1c/index.html 8.1中文文档]<br />
*[http://wiki.postgresql.org/images/7/7b/PostgreSQL8.2.3doc.zip 8.2.3中文文档]<br />
*[http://developer.postgresql.org/pgdocs/postgres/index.html 9.1文档]<br />
*[http://wiki.postgresql.org/wiki/Help:Formatting wiki语法参考]<br />
<br />
校对的时候,使用<del> </del>标记原来的内容,然后写上修改后的东西。<br />
{|border="1"<br />
|其他语言模块<br />
|模块名字<br />
|标注者<br />
|-<br />
|python<br />
|[[psycopy]]<br />
|冇得弹<br />
|-<br />
|.net<br />
|[[npgsql]]<br />
|冇得弹<br />
|-<br />
|php<br />
|直接支持<br />
|冇得弹<br />
|}<br />
翻译任务<br />
{| border=1<br />
| 部分 <br />
| 章节<br />
| 标题<br />
| 翻译志愿者<br />
| 状态<br />
| 开始时间<br />
| 计划完成时间<br />
| 备注<br />
| 文件<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 1<br />
| Getting Started<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
| [[9.1第一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 2<br />
| The SQL Language<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第二章]]<br />
|-<br />
| Tutorial<br />
| 3<br />
| Advanced Features<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第三章]]<br />
|-<br />
| The SQL Language<br />
| 4<br />
| SQL Syntax<br />
| 冇得弹<br />
| 89%<br />
|<br />
|<br />
|<br />
|[[9.1第四章]]<br />
|-<br />
| The SQL Language<br />
| 5<br />
| Data Definition<br />
| 冇得弹<br />
| 98%<br />
|<br />
|<br />
|<br />
|[[9.1第五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| The SQL Language<br />
| 6<br />
| Data Manipulation<br />
| 冇得弹<br />
| '''完美'''<br />
| <br />
| <br />
| 这章的内容和8.1文档一样<br />
|[[9.1第六章]]<br />
|-<br />
| The SQL Language<br />
| 7<br />
| Queries<br />
| 冇得弹<br />
| <br />
| <br />
| <br />
| <br />
|[[9.1第七章]]<br />
|-<br />
| The SQL Language<br />
| 8<br />
| Data Types<br />
| 冇得弹<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第八章]]<br />
|-<br />
| The SQL Language<br />
| 9<br />
| Functions and Operators<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第九章]]<br />
|-<br />
| The SQL Language<br />
| 10<br />
| Type Conversion<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十章]]<br />
|-<br />
| The SQL Language<br />
| 11<br />
| Indexes<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十一章]]<br />
|-<br />
| The SQL Language<br />
| 12<br />
| Full Text Search<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十二章]]<br />
|-<br />
| The SQL Language<br />
| 13<br />
| Concurrency Control<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十三章]]<br />
|-<br />
| The SQL Language<br />
| 14<br />
| Performance Tips<br />
| 冇得弹<br />
|开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十四章]]<br />
|-<br />
| Server Administration<br />
| 15<br />
| Installation from Source Code<br />
| 紫轩<br />
| 基本完成<br />
|<br />
|<br />
|<br />
|[[9.1第十五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 16<br />
| Installation from Source Code on Windows<br />
| 紫轩<br />
| 完成 校正中 (iihero补校)<br />
|<br />
|<br />
| 有些计算机专业用语不懂,恐怕会让人误解<br />
|[[9.1第十六章]]<br />
|-<br />
| Server Administration<br />
| 17<br />
| Server Setup and Operation<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十七章]]<br />
|-<br />
| Server Administration<br />
| 18<br />
| Server Configuration<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十八章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 19<br />
| Client Authentication<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第十九章]]<br />
|-<br />
| Server Administration<br />
| 20<br />
| Database Roles<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十章]]<br />
|-<br />
| Server Administration<br />
| 21<br />
| Managing Databases<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 22<br />
| Localization<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十二章]]<br />
|-<br />
| Server Administration<br />
| 23<br />
| Routine Database Maintenance Tasks<br />
| 小燕子 <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十三章]]<br />
|-<br />
| Server Administration<br />
| 24<br />
| Backup and Restore<br />
| 星海游侠<br />
| 完成了一半<br />
|<br />
|<br />
|<br />
|[[9.1第二十四章]]<br />
|-<br />
| Server Administration<br />
| 25<br />
| High Availability, Load Balancing, and Replication<br />
| 星海游侠<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十五章]]<br />
|-<br />
| Server Administration<br />
| 26<br />
| Recovery Configuration<br />
| 星海游侠<br />
| 完成,待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十六章]]<br />
|-<br />
| Server Administration<br />
| 27<br />
| Monitoring Database Activity<br />
| 泥鳅<br />
| 完成<br />
| <br />
|<br />
| 本章的中文部分是基于9.0的,9.1新增的内容没有翻译还是英文<br />
|[[9.1第二十七章]]<br />
|-<br />
| Server Administration<br />
| 28<br />
| Monitoring Disk Usage<br />
| 泥鳅<br />
| 完成,很简单,不需要校验<br />
|<br />
|<br />
|<br />
|[[9.1第二十八章]]<br />
|-<br />
| Server Administration<br />
| 29<br />
| Reliability and the Write-Ahead Log<br />
| 小燕子<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十九章]]<br />
|-<br />
| Server Administration<br />
| 30<br />
| Regression Tests<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十章]]<br />
|-style="font-style:italic;color:green;"<br />
| Client Interfaces<br />
| 31<br />
| ibpq - C Library<br />
| 泥鳅<br />
| Working in progress<br />
| 2011-7-17<br />
| 工作量极大,不好估算时间<br />
|<br />
|[[9.1第三十一章]]<br />
|-<br />
| Client Interfaces<br />
| 32<br />
| Large Objects<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十二章]]<br />
|-<br />
| Client Interfaces<br />
| 33<br />
| ECPG - Embedded SQL in C<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十三章]]<br />
|-<br />
| Client Interfaces<br />
| 34<br />
| The Information Schema<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十四章]]<br />
|-style="font-style:italic;color:red;"<br />
| Server Programming<br />
| 35<br />
| Extending SQL<br />
| iihero<br />
| '''Start working'''<br />
|<br />
|<br />
|<br />
|[[9.1第三十五章]]<br />
|-<br />
| Server Programming<br />
| 36<br />
| Triggers<br />
| 任洪彩<br />
| 待校<br />
| <br />
|<br />
|<br />
|[[9.1第三十六章]]<br />
|-<br />
| Server Programming<br />
| 37<br />
| The Rule System<br />
| 小燕子<br />
| i'm here<br />
|<br />
|<br />
|<br />
|[[9.1第三十七章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Programming<br />
| 38<br />
| Procedural Languages<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第三十八章]]<br />
|-<br />
| Server Programming<br />
| 39<br />
| PL/pgSQL - SQL Procedural Language<br />
| 杨拼<br />
|开始了。。。<br />
|<br />
|<br />
|<br />
|[[9.1第三十九章]]<br />
|-<br />
| Server Programming<br />
| 40<br />
| PL/Tcl - Tcl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十章]]<br />
|-<br />
| Server Programming<br />
| 41<br />
| PL/Perl - Perl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十一章]]<br />
|-<br />
| Server Programming<br />
| 42<br />
| PL/Python - Python Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十二章]]<br />
|-<br />
| Server Programming<br />
| 43<br />
| Server Programming Interface<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十三章]]<br />
|-<br />
| Reference<br />
| I<br />
| SQL Commands<br />
| gvim<br />
| 开始 23/150<br />
|<br />
|<br />
|<br />
|[[9.1参考1]]<br />
|-<br />
| Reference<br />
| II<br />
| PostgreSQL Client Applications<br />
| eastday<br />
| 刚刚开始。<br />
|<br />
|<br />
|大多内容和8.1相同.<br />
|[[9.1参考2]]<br />
|-<br />
| Reference<br />
| III<br />
| PostgreSQL Server Applications<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1参考3]]<br />
|- style="font-style:italic;color:red;"<br />
| Internals<br />
| 44<br />
| Overview of PostgreSQL Internals<br />
| Galy<br />
| 着手<br />
|<br />
|<br />
|<br />
| [[9.1第44章]]<br />
|-<br />
| Internals<br />
| 45<br />
| System Catalogs<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第45章]]<br />
|-<br />
| Internals<br />
| 46<br />
| Frontend/Backend Protocol<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第46章]]<br />
|-<br />
| Internals<br />
| 47<br />
| PostgreSQL Coding Conventions<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第47章]]<br />
|-<br />
| Internals<br />
| 48<br />
| Native Language Support<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第48章]]<br />
|-<br />
| Internals<br />
| 49<br />
| Writing A Procedural Language Handler<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第49章]]<br />
|-<br />
| Internals<br />
| 50<br />
| Genetic Query Optimizer<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第50章]]<br />
|-<br />
| Internals<br />
| 51<br />
| Index Access Method Interface Definition<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第51章]]<br />
|-<br />
| Internals<br />
| 52<br />
| GiST Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第52章]]<br />
|-<br />
| Internals<br />
| 53<br />
| GIN Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第53章]]<br />
|-<br />
| Internals<br />
| 54<br />
| Database Physical Storage<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第54章]]<br />
|-<br />
| Internals<br />
| 55<br />
| BKI Backend Interface<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第55章]]<br />
|-<br />
| Internals<br />
| 56<br />
| How the Planner Uses Statistics<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第56章]]<br />
|-<br />
|}<br />
<br />
志愿者简介<br />
<br />
感谢为我们的文档付出了的无私的奉献。<br />
{|border=1<br />
|网名来胳<br />
|猫<br />
|-<br />
|''泥鳅''<br />
|<br />
|-<br />
|''小神厨'' <br />
|anliulin1986 _at_ sina.com<br />
|-<br />
|''Galy''<br />
|galylee _at_ gmail.com<br />
|-<br />
|''hxre''<br />
|hxre _at_ hotmail.com<br />
|-<br />
|''冇得弹''<br />
|zelda_ok _at_ 163.com<br />
|-<br />
|星海游侠<br />
|<br />
|-<br />
|''iihero''<br />
|iihero _at_ qq.com<br />
|}<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208209.1参考32013-09-20T07:46:23Z<p>Horen: /* postmaster */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
<br />
'''Name'''<br />
<br />
pg_ctl -- 初始化、启动、停止和重起 PostgreSQL<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_ctl init[db] [-s] [-D datadir] [-o initdb-options]</pre><br />
<br />
<pre>pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]</pre><br />
<br />
<pre>pg_ctl stop [-W] [-t seconds] [-s] [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ]</pre><br />
<br />
<pre>pg_ctl restart [-w] [-t seconds] [-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o options]</pre><br />
<br />
<pre>pg_ctl reload [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl status [-D datadir]</pre><br />
<br />
<pre>pg_ctl promote [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl kill signal_name process_id</pre><br />
<br />
<pre>pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]</pre><br />
<br />
<pre>pg_ctl unregister [-N servicename]</pre><br />
<br />
'''Description'''<br />
<br />
pg_ctl 是一个用于初始化,启动,停止, 或者重起 PostgreSQL 后端服务器(postgres), 或者显示一个运行着的服务器的状态的工具, 尽管我们可以手动启动服务器,但是 pg_ctl 封装了重新定向日志输出,与终端和进程组合理分离,以及另外提供了方便的选项用于有控制的关闭。<br />
<br />
在Init或initdb模式下创建一个新的PostgreSQL数据库集群。一个数据库集群是由一个服务进程管理的多个数据库集合。该模式调用initdb命令。详情请参考initdb。<br />
<br />
在 start 模式里会启动一个新的服务器。服务器是在后台启动的,标准输入被附着到了 /dev/null(在Windows下是nul) 上。在类UNIX系统中,缺省情况下服务器的标准输出和标准错误输出都被发送到pg_ctl's的标准输出(不是标准错误)。pg_ctl的标准输出被重定向到一个文件或者通过管理输送给另一个进行,比如日志处理程序rotatelogs。否则postgres将把它的输出写到控制终端(从后台)并且不会脱离shell进程组。在Windows中,缺省情况下服务器的标准输出和标准错误被发送到终端。这些缺省行为可以通过-l选项来指定log重定向到某个文件。推荐使用-l或者输出重定向。<br />
<br />
在 stop 模式下,那个正在特定数据目录运行的服务器被关闭。你可以用 -m 选项选择三种不同的关闭模式:"Smart" 模式(也是缺省模式)等待所有客户端中断联接且正在进行的备份将被终止。如果服务器不是hot standby,恢复和流复制也将被在客户端连接中断后退出。"Fast" 模式并不等待客户端中断联接。 所有活跃事务都被回滚并且客户端都强制断开。 "Immediate" 模式将在没有干净关闭的情况下退出。这么做将导致在重新启动的时候的恢复。<br />
<br />
restart 实际上是先执行一个停止,然后紧跟一个启动。它允许变换postgres命令行的选项。<br />
<br />
reload 模式简单地给postmaster发送一个 SIGHUP 信号,导致它重新读取她的配置文件 (postgresql.conf,pg_hba.conf 等等) 这样就允许修改配置文件选项而不用完全重启系统来使之生效。<br />
<br />
status 模式检查一个服务器是否在指定的数据目录运行, 如果是,那么显示其 PID 和调用它的命令行选项。<br />
<br />
生产模式,运行在某个数据目录的standby服务器将退出恢复并进开始进行读写操作。<br />
<br />
kill 模式允许你给一个指定的进程发送信号。这个功能对 Microsoft Windows 特别有用,因为那里没有 kill 命令。 使用 --help 查看支持的信号的名字的列表。<br />
<br />
register 模式允许你在 Microsoft Windows 上注册一个系统服务。-S选项用于设定启运类型,或者“自动”(系统启动时自动启动)或者“后台”(后台运行)。<br />
<br />
unregister 模式允许你在 Microsoft Windows 上删除这个系统服务, 这个系统服务是前面用 register 命令注册的。<br />
<br />
'''Options'''<br />
<br />
-c<br />
:允许服务器崩溃时产生core文件,只要其所在的平台允许,所有的信息将会放进core文件中。异常进程产生的堆栈信息对于调试或问题诊断非常有用。<br />
<br />
-D datadir<br />
:声明该数据库文件的文件系统位置。 如果忽略这个选项,使用环境变量 PGDATA。<br />
<br />
-l filename<br />
:把服务器日志输出附加在 filename 文件上。 如果该文件不存在,那么创建它。umask设置为 077, 因此缺省时是不允许从其它用户向日志文件访问的。<br />
<br />
-m mode<br />
:声明关闭模式。mode 可以是smart, fast, 或者 immediate,或者是这三个之一的第一个字母。若不指定,缺省是smart模式。<br />
<br />
-o options<br />
:声明要直接传递给 postgres 的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-o initdb-options<br />
:指定要直接传递给initdb命令行的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-p path<br />
:声明 postgres 可执行文件的位置。缺省时postgres是从和pg_ctl相同的目录取出,如果不是, 那么就是写死的安装目录。除非你想干点什么特别的事情,并且得到类似没有找到postgres这样的错误,否则没有必要使用这个选项。<br />
<br />
:在init模式,该选项相同的用来指定initdb可执行文件的位置。<br />
<br />
-s<br />
:只打印错误,而不打印提示性信息。<br />
<br />
-t<br />
:等待启动或关闭完成的最大秒数。缺省是60秒。<br />
<br />
-w<br />
:等待启动或者关闭的完成。 这个参数是关闭时的缺省值,但不是启动时的缺省值。等待启动时,pg_ctl不断地尝试连接服务器。等待关闭时,pg_ctl等待服务器删除其PID文件。pg_ctl基于启动或关闭成功与否返回退出代码。<br />
<br />
-W<br />
:不等待启动或者停止的完成。这是启动和重起的缺省。<br />
<br />
'''Options for Windows'''<br />
<br />
-N servicename<br />
:要注册的系统服务的名字。这个名字将用于服务名和显示名。<br />
<br />
-P password<br />
:用户启动服务的口令。<br />
<br />
-S start-type<br />
:注册系统服务的启动类型。启动类型可以是auto或demand,或者二者的首字母。如果不指定,缺省值是auto.<br />
<br />
-U username<br />
:启动服务的用户名。对于domain用户,格式为DOMAIN\username.<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
pg_ctl 像其他PostgreSQL实用工具一样,同样使用libpq支持的环境变量(参照Section 31.13)。其他的变量,参照postgres.<br />
<br />
'''Files'''<br />
<br />
postmaster.pid<br />
:该文件是否存在用于帮助pg_ctl判断服务器是否正在运行。<br />
<br />
postmaster.opts<br />
:如果该文件存在于数据目录,pg_ctl(在启动模式下)将以该文件的内容为参数传递给postgres,除非被-o选项覆盖。该文件内容也会在状态模式下显示。<br />
<br />
'''Examples'''<br />
<br />
'''Starting the Server'''<br />
<br />
启动服务器:<br />
<pre>$ pg_ctl start</pre><br />
<br />
启动服务器,等待至服务器接受连接:<br />
<pre>$ pg_ctl -w start</pre><br />
<br />
使用端口5433启动服务器,同时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" start</pre><br />
<br />
'''Stopping the Server'''<br />
<br />
关闭服务器:<br />
<pre>$ pg_ctl stop</pre><br />
<br />
选项-m用来控制服务器关闭方式:<br />
<pre>$ pg_ctl stop -m fast</pre><br />
<br />
'''Restarting the Server'''<br />
<br />
服务器重启几乎等价于关闭后再启动,只是pg_ctl保存并重用了传递给之前进程的命令行选项。最简单的重启:<br />
<pre>$ pg_ctl restart</pre><br />
<br />
重启服务器并等待其关闭并重启:<br />
<pre>$ pg_ctl -w restart</pre><br />
<br />
使用端口5433重启,重启时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" restart</pre><br />
<br />
'''Showing the Server Status'''<br />
<br />
下面是简单的pg_ctl状态输出例子:<br />
<pre><br />
$ pg_ctl status<br />
pg_ctl: server is running (PID: 13718)<br />
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"<br />
</pre><br />
<br />
This is the command line that would be invoked in restart mode.<br />
<br />
'''See Also'''<br />
<br />
initdb, postgres<br />
<br />
== pg_resetxlog ==<br />
<br />
'''Name'''<br />
<br />
重置一个 PostgreSQL 数据库集群的预写日志以及其它控制内容<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_resetxlog [-f] [-n] [-ooid ] [-x xid ] [-e xid_epoch ] [-m mxid ] [-O mxoff ] [-l timelineid,fileid,seg ] datadir</pre><br />
<br />
'''Description'''<br />
<br />
pg_resetxlog 清理预写日志(WAL)并且可以选择地重置其它一些存储在 pg_control 文件中的控制信息。 有时候,如果这些文件崩溃了,我们需要这个功能。 我们一定只把它用作最后的方法,就是说只有因为这样的崩溃导致服务器无法启动的时候才使用。<br />
<br />
在运行这个命令之后,我们可能可以启动服务器了,但是, 一定要记住数据库可能因为部分提交的事务而含有不完整的数据。 你应该马上转储你的数据,运行 initdb,然后重新装载。 在重新装载之后,检查不完整的部分然后根据需要进行修复。<br />
<br />
这个命令只能由安装服务器的用户运行,因为它需要对数据目录的读写权限。 出于安全考虑,你必须在命令行上声明数据目录。 pg_resetxlog 不使用环境变量 PGDATA。<br />
<br />
如果 pg_resetxlog 抱怨说它无法判断用于 pg_control 的有效数据,那么你可以强制它继续处理, 方法是声明 -f (强制)开关。在这种情况下,那些丢失了的数据的值将用模糊的近似数值代替。 大多数字段都可以匹配上,但是下一个 OID,下一个事务 ID,下一个多事务 ID (两阶段提交的东西)和偏移量, WAL 开始地址以及数据库区域字段可能需要手工帮助, 前面几个可以用下面讨论的开关设置。 如果你不能判断所有这些字段的正确数值,那么还是可以使用 -f, 但是这样恢复过来的数据库更要怀疑有问题:立即转储和重置是必须的。 在转储之前不要执行任何修改数据的操作,因为任何这样的动作都可能把事情搞得更糟糕。<br />
<br />
-o,-x,-m,-O 和 -l 开关允许我们手工设置下一个 OID,下一个事务 ID,下一个多事务 ID, 下一个多事务偏移量,以及 WAL 起始位置的数值。只有在 pg_resetxlog 无法通过读取 pg_control 判断合适的数值的时候才需要它。安全的数值可以用下面方法判断:<br />
<br />
:对于下一个事务 ID 而言,一个安全的数值是看看数据目录里的 /pg_clog 里数值最大的文件名, 然后加一,然后再乘上 1048576。 请注意那些文件名是十六进制的。通常我们也以十六进制的形式声明开关值是最简单的。 比如,如果 0011 是 pg_clog 里最大的记录, -x 0x1200000 就可以了(后面的五个零提供了合适的乘积)。<br />
<br />
:下一个多事务 ID (-m)的安全值可以通过查看数据目录里 pg_multixact/offsets 子目录里面的数字最大的文件名,加一,然后乘以 65536 得到。 和上面一样,文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值, 然后在后面加四个零。<br />
<br />
:下一个多事务偏移量(-O)的安全值可以通过检查数目目录里 pg_multixact/members 子目录下的数字最大的文件名,加一,乘以 65536 得到。和上面一样, 文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值,然后背后加四个零。<br />
<br />
:WAL 的起始位置(-l)应该比目前存在于数据目录里得 /pg_xlog 里面的任何文件号都大。它的文件名也是十六进制的,并且有三部分。 第一部分是"时间线 ID",通常应该保持相同。 第三部分不要选择大于 255(0xFF); 应该是在达到 255 的时候给第二部分增一然后重置第三部分为 0。 比如,如果 00000001000000320000004A 是 pg_xlog 里最大的条目, 那么-l 0x1,0x32,0x4B 就可以了; 但如果最大的条目是 000000010000003A000000FF, 选择 -l 0x1,0x3B,0x0 或更多。<br />
<br />
<pre><br />
注意:pg_resetxlog本身看起来在pg_xlog文件里并且选择默认的-l选项来设置为最后存在的文件名。因此,只有明确知道WAL段文件并没有在当前pg_xlog里才可以手动调整-l,比如这些条目在离线的存档里或者pg_xlog内容完全丢失。<br />
</pre><br />
<br />
:我们没有很容易的办法来判断比数据库中最大的 OID 大一号的下一个 OID, 不过很走运的是获取正确的下一个 OID 并非非常关键的事情。<br />
<br />
:事务ID时间轴事实上并不存在于数据库的任何地方,只存在pg_resetxlog设置字段里,所以只要数据库接受都可以工作。你可能需要调整该值来确保复制系统,比如Slony-I,工作正常,这样的话需要从下游复制数据库状态里获得合适的值。<br />
<br />
开关 -n (无操作)指示 pg_resetxlog 打印从 pg_control 重新构造的数值然后不修改任何值就退出。 这主要是一个调试工具,但是在 pg_resetxlog 真正处理前进行的整洁性检查的时候可能会有用。<br />
<br />
-V 和--version选项打印pg_resetxlog版本信息并退出。-?和--help选项输出支持的参数并退出。<br />
<br />
'''Notes'''<br />
<br />
在 postmaster 运行的时候一定不要运行这个命令。 如果发现在数据文件目录里有锁文件,那么 pg_resetxlog 将拒绝启动。如果 postmaster 崩溃,那么可能会剩下一个锁文件; 如果这样,你可以删除该锁文件以便允许 pg_resetxlog 运行。但是在你这么做之前,一定要确信没有任何postmaster或者后端服务器仍在运行。<br />
<br />
== postgres ==<br />
<br />
'''Name'''<br />
<br />
PostgreSQL数据库服务器<br />
<br />
'''Synopsis'''<br />
<br />
<pre>postgres [option...]</pre><br />
<br />
'''Description'''<br />
<br />
postgres 是PostgreSQL数据库服务器。客户端应用为了访问数据库必须连接(通过网络或本地)到一个运行的postgres实例。postgres实例启动一个独立的服务来处理连接。<br />
<br />
一个postgres实例通常管理着整个数据库集群的数据。一个数据库集群是存储在常见文件系统中某个位置(数据区)的数据库集合。同时可以有多个postgres实例运行,只要它们使用不同的数据区以及不同的通信端口(看下面)。postgres需要知道数据区的位置才可以启动。位置必须通过-D选项或者PGDATA环境变量指定,没有缺省值。通常,-D或者PGDATA直接指向initdb创建的数据目录。其他可能的文件布局在18.2章节里讨论过。<br />
<br />
缺省情况下postgres在前台启动并打印日志到标准错误流。实际应用中postgres应该在后台启动,也许是系统启动时。<br />
<br />
postgres命令也可以以单用户模式调用。主要用途是在自举时被initdb调用。有时用于调式或灾难恢复,需要注意的是运行单用户服务器并不真的适合调试服务器,因为没有真实的进程间通信以及锁发生。一旦从shell中进入单用户模式,用户可以进行查询并且查询结果会直接输出到屏幕,但是输出格式对开发者有用但不是终端用户。在单用户模式下,会话用户ID将被设置为1并且隐含的超级用户权限也将授予该用户。该用户事实上不需要退出,所以单用户模式可以用来从某种系统表的灾难损坏中手动恢复。<br />
<br />
'''Options'''<br />
<br />
postgres接受下面的命令行参数。这些参数的详细讨论请查阅18章。如不想每次都敲这些参数,可以将其保存到一个配置文件。一些(安全)选项也可以通过应用特殊的方式设置,这些设置仅针对当前会话。例如,如果设置了PGOPTIONS环境变量,基于libpq的客户端将传递该字符串到服务器,基将被解释成postgres命令行选项。<br />
<br />
'''General Purpose'''<br />
<br />
-A 0|1<br />
:打开运行时断言检查,这是一个帮助发现程序错误的调试手段。该选项只在PosgreSQL被编译阶段开启了断言检查才有效。如打开了,那么断言检查缺省是开启的。<br />
<br />
-B nbuffers<br />
:设置服务进程可以使用的共享缓冲区的大小。initdb自动选定该参数的缺省值。指定该选项等同于设置share_buffers的配置参数。<br />
<br />
-c name=value<br />
:设置一个命名的运行时参数。PostgreSQL 支持的配置参数在 Chapter 18 里描述。 大多数其他命令行选项实际上都是这样的参数赋值的短形式。 -c 可以出现多次用以设置多个参数。<br />
<br />
-d debug-level<br />
:设置调试级别。数值越高,写到服务器日志的调试输出越多。 数值范围是 1 到 5。也可以为特定的会话设置-d 0,以此来阻止postgres父进程输出日志到当前会话。<br />
<br />
-D datadir<br />
:声明数据目录或者配置文件的文件系统路径。细节详见 Section 18.2。<br />
<br />
-e<br />
:把缺省日期风格设置为 "European",也就是说用"DMY"规则解释日期输入, 并且在一些日期输出格式里日子在月份前面打印。 参阅 Section 8.5 获取更多细节。<br />
<br />
-F<br />
:关闭 fsync 调用,提高性能,但是要冒系统崩溃时数据毁坏的风险。 声明这个选项等效关闭了 fsync 参数。在使用之前阅读详细文档!<br />
<br />
-h hostname<br />
:声明监听来自前端应用 TCP/IP 联接的 IP 主机名或地址。 数值也可以是一个用逗号分隔的地址列表,或者*表示监听所有可用的地址。空值表示不监听任何 IP 地址,而只是使用 Unix 域套接字用于与服务器连接。 缺省的时候是只监听 localhost。 声明这个选项等效于设置 listen_addresses 配置参数。<br />
<br />
-i<br />
:这个选项打开 TCP/IP (网际域套接字)通讯。 没有这个选项,只能接受本地连接。 这个选项等效于在 postgresql.conf 中或者通过 -h 设置 listen_addresses 为 *。<br />
<br />
:这个选项已经废弃了,因为它不能实现 listen_addresses 的所有功能。 我们最好直接设置 listen_addresses。<br />
<br />
-k directory<br />
:声明侦听着等待来自前端应用联接的 Unix 域套接字的位置。 缺省通常是 /tmp,但是可以在编包的时候修改。<br />
<br />
-l<br />
:这个选项打开用 SSL 进行的安全通讯。要使用这个选项,编译PostgreSQL时你必须打开了 SSL 选项。 有关使用 SSL 的信息,请参考 Section 17.9。<br />
<br />
-N max-connections<br />
:设置服务器最大可接受客户端的连接数。initdb自动选择缺省值。声明这个选项等效于声明 max_connections 配置参数。<br />
<br />
-o extra-options<br />
:在 extra-options 里声明的 postgres 选项都传递给所有由这个postgres 启动的服务进程。如果选项字串包含任何空格,整个字串必须引起来。<br />
<br />
:该选项已被废弃,所有用于服务进程的命令行选项都可以直接指定。<br />
<br />
-p port<br />
:声明监听着等待客户端应用连接的互联网 TCP/IP 端口或一个本地 Unix 域套接字文件扩展(描述符)。 缺省的端口号是环境变量 PGPORT 的值。如果没有设置 PGPORT,缺省是编译时建立的值(通常是 5432)。 如果你声明了一个非缺省端口,那么所有客户端应用都必须用命令行选项或者 PGPORT 声明同一个端口。<br />
<br />
-s<br />
:在每条命令结束时打印时间信息和其他统计信息。 这个开关对测试性能和调节缓冲区数量有好处。<br />
<br />
-S work-mem<br />
:声明内部排序和散列临时磁盘文件时可以使用的内存数量。 参阅 Section 18.4.1 里描述的配置变量 work_mem。<br />
<br />
--name=value<br />
:设置一个命名的运行时参数;其缩写形式是 -c。<br />
<br />
--describe-config<br />
:这个选项以 tab 分隔的 COPY 格式,倒出服务器内部配置变量,描述和缺省值。 设计它主要是给管理工具使用。<br />
<br />
'''Semi-internal Options'''<br />
<br />
该选项在这里描述主要用于调试目的,在某些场合下帮助恢复严重损坏的数据库。构建产品数据时没理由使用它们。它们列在这只被PostgreSQL系统开发者使用。而且,这些选项在将来的发布中可能不通知地改变或移除。<br />
<br />
-f { s | i | m | n | h }<br />
:禁用特定的扫描和连接方法:s和i分别禁用连续和索引扫描,n、m和h分别禁用嵌入式循环、合并和哈希连接。<br />
<br />
:连续扫描和嵌入式循环都不可能被完全禁止;-fs和-fm选项只是简单地阻止优化器使用该方案。<br />
<br />
-n<br />
:该选项用于调试导致服务器异常挂机问题。这种情况下的策略是通知所有的服务器进程必须中断并且重新初始化共享内存和信号量。因为某个错误的服务器进程可能已经损坏了共享状态。该选项指定postgres不重新初始化共享数据结构。一个有经验的系统程序员可以使用调试器来检查共享内存和信号量状态。<br />
<br />
-O<br />
:允许系统表结构可以被修改。该选项被initdb使用。<br />
<br />
-P<br />
:在读系统表时忽略索引,但是修改这些表时更新索引。这在恢复损坏的系统索引时非常有用。<br />
<br />
-t pa[rser] | pl[anner] | e[xecutor]<br />
:查询主要的系统模块时输出时间统计信息。该选项不能与-s选项一起使用。<br />
<br />
-T<br />
:该选项用于调试导致服务器异常挂机问题。这种情况下的策略是通知所有的服务器进程必须中断并且重新初始化共享内存和信号量。因为某个错误的服务器进程可能已经损坏了共享状态。该选项指定postgres通过发送SIGSTOP来终止所有服务器进程,但是不会导致它们终止。这允许系统程序员手动从服务器进程收集core dumps<br />
<br />
-v protocol<br />
:为特定的会话指定前端和后端使用的协议版本。该选项只做内部使用。<br />
<br />
-W seconds<br />
:设置某个新的服务器进程启动并完成鉴权过程后延迟的秒数。主要提供attach上服务器进程进行debug的机会。<br />
<br />
'''Options for Single-User Mode'''<br />
<br />
下面的选项只提供给单用户模式。<br />
<br />
--single<br />
:选择单用户模式。这必须是命令行选项的第一个参数。<br />
<br />
database<br />
:指定访问的数据库名字。这必须是命令行最后一个参数。如果没有该参数将缺省使用用户名。<br />
<br />
-E<br />
:回显所有的命令。<br />
<br />
-j<br />
:禁用换行符作为语句的分隔符。<br />
<br />
-r filename<br />
:发送所有的服务端日志到某个文件。在正常的多用模式该选项被忽略了,所有进程使用标准错误输出。<br />
<br />
'''Environment'''<br />
<br />
PGCLIENTENCODING<br />
:客户端使用的确缺省字符编码。(客户端可以独立地覆盖这个。)这个值也可以在配置文件里设置。<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
PGDATESTYLE<br />
:运行时参数 DateStyle 的缺省值。(已经不再鼓励使用环境变量了。)<br />
<br />
PGPORT<br />
:缺省端口(最好再配置文件中设置)<br />
<br />
TZ<br />
:服务器时区<br />
<br />
'''Diagnostics'''<br />
<br />
一旦提到了 semget 或者 shmget 的错误信息可能意味着你需要配置你的内核, 提供足够的共享内存和信号量。更多讨论,参阅 Section 17.4。你也可以通过降低 shared_buffers 参数值以减少 PostgreSQL的共享内存的消耗, 或者降低max_connections的参数值减少PostgreSQL的信号量的消耗, 以此可以推迟重新配置内核。<br />
<br />
如果错误信息提示有另外一个服务端已经运行,你就需要仔细检查,比如使用下面的命令:<br />
<br />
<pre>$ ps ax | grep postgres</pre><br />
<br />
or<br />
<br />
<pre>$ ps -ef | grep postgres</pre><br />
<br />
具体使用哪种取决于你的系统。如果确信没有冲突的在运行,那么你可以删除消息里提到的锁文件然后再次运行。<br />
<br />
如果错误信息提示不能绑定某个端口,可以是该端口已被其他非PostgreSQL进程占用。如果你终断了postgres并立即重启且使用了相同的端口也可以遇到这个错误,你需要等几秒终让操作系统关闭这个端口再试。最后,如果你使用了操作系统保留端口也可能遇到错误。比如,很多版本的Unix将端口号小于1024的设为信任并只允许Unix超级用户访问。<br />
<br />
'''Notes'''<br />
<br />
工具命令 pg_ctl 可以用于安全而有效地启停 postgres。<br />
<br />
如果有可能,不要使用 SIGKILL杀死 postgres。 这样会阻止其在退出前释放它持有的系统资源(例如共享内存和信号灯)。这样新的postgres运行时可能会有问题。<br />
<br />
要正常结束 postgres,可以使用信号 SIGTERM,SIGINT, 或 SIGQUIT。 第一个信号将等待所有的客户端退出后才退出。 第二个将强制断开所有客户端,而第三个将不停止立刻退出, 导致在重起时的恢复运行。<br />
<br />
信号SIGHUP将重新加载服务端配置文件。也可以给某个独立的服务进程发SIGHUP,但通常感知不到变化。<br />
<br />
要停止运行一个查询,使用 SIGINT 给正在运行该命令的 postgres 进程发信号。<br />
<br />
postgres服务器使用SIGTERM通知下属的服务进程正常退出,使用SIGQUIT强制退出并不做正常的清理。这些信号不应该被用户使用。给服务进程发送SIGKILL信号是不明智的,postgres主进程将以崩溃终止并且强制让兄弟进程也退出,方便进行崩溃恢复过程。<br />
<br />
'''Bugs'''<br />
<br />
--选项在FreeBSD或者OpenBSD上无效。使用-c替代。这在受影响的操作系统上是个bug;将来的发布如果还不能解决该问题将提供一个修补方案。<br />
<br />
'''Usage'''<br />
<br />
启动一个单用户模式服务器,使用命令<br />
<br />
<pre>postgres --single -D /usr/local/pgsql/data other-options my_database</pre><br />
<br />
用 -D 给服务器提供正确的数据库目录的路径, 或者确保环境变量 PGDATA 已经正确设置。 同时还要声名你想用的特定数据库名字。<br />
<br />
通常,独立运行的服务器把换行符当做命令输入完成字符; 它还不懂分号的作用,因为那些东西是在 psql 里的。 要想把一行分成多行写,你必需在除最后一个换行符以外的每个换行符前面敲一个反斜杠。<br />
<br />
但是如果你使用 -j 命令行开关,那么换行符就不会中止命令输入。 服务器将从标准输入读取数据,直到碰到文件结尾(EOF), 然后把输入当做一个单个的命令字串处理。这个时候反斜杠-换行符就不再当做特殊情况处理。<br />
<br />
要退出会话,键入EOF通常是 Control+D)。 如果你已经使用了 -j,需要用两个连续的EOF来退出。<br />
<br />
请注意独立运行的服务器不会提供复杂的行编辑功能(比如,没有命令行历史)。单用户模式也不做任何后台处理,比如自动检查。<br />
<br />
'''Examples'''<br />
<br />
后台启动postgres并使用缺少参数:<br />
<pre>$ nohup postgres >logfile 2>&1 </dev/null &</pre><br />
<br />
用特定端口,如1234启动postgres:<br />
<pre>$ postgres -p 1234</pre><br />
<br />
使用psql连接到服务端,同时使用-p选项指定端口:<br />
<pre>$ psql -p 1234</pre><br />
<br />
或者也可以设置环境变量PGPORT:<br />
<br />
<pre><br />
$ export PGPORT=1234<br />
$ psql<br />
</pre><br />
<br />
命名的运行时参数可以使用下面的方式之一:<br />
<br />
<pre><br />
$ postgres -c work_mem=1234<br />
$ postgres --work-mem=1234<br />
</pre><br />
<br />
任何一种形式都将覆盖postgresql.conf文件中的work_mem设置。请注意在参数名里的下划线在命令行上可以写成下划线,也可以写成划线。除了短期试验外,推荐使用postgresql.conf而不是打开命令行开关来设置参数。<br />
<br />
'''See Also'''<br />
<br />
initdb, pg_ctl<br />
<br />
== postmaster ==<br />
<br />
'''Name'''<br />
<br />
postmaster -- PostgreSQL database server<br />
<br />
'''Synopsis'''<br />
<br />
<pre>postmaster [option...]</pre><br />
<br />
'''Description'''<br />
<br />
postmaster是个不推荐使用的postgres别名。<br />
<br />
'''See Also'''<br />
<br />
postgres</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208199.1参考32013-09-20T07:42:51Z<p>Horen: /* postgres */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
<br />
'''Name'''<br />
<br />
pg_ctl -- 初始化、启动、停止和重起 PostgreSQL<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_ctl init[db] [-s] [-D datadir] [-o initdb-options]</pre><br />
<br />
<pre>pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]</pre><br />
<br />
<pre>pg_ctl stop [-W] [-t seconds] [-s] [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ]</pre><br />
<br />
<pre>pg_ctl restart [-w] [-t seconds] [-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o options]</pre><br />
<br />
<pre>pg_ctl reload [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl status [-D datadir]</pre><br />
<br />
<pre>pg_ctl promote [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl kill signal_name process_id</pre><br />
<br />
<pre>pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]</pre><br />
<br />
<pre>pg_ctl unregister [-N servicename]</pre><br />
<br />
'''Description'''<br />
<br />
pg_ctl 是一个用于初始化,启动,停止, 或者重起 PostgreSQL 后端服务器(postgres), 或者显示一个运行着的服务器的状态的工具, 尽管我们可以手动启动服务器,但是 pg_ctl 封装了重新定向日志输出,与终端和进程组合理分离,以及另外提供了方便的选项用于有控制的关闭。<br />
<br />
在Init或initdb模式下创建一个新的PostgreSQL数据库集群。一个数据库集群是由一个服务进程管理的多个数据库集合。该模式调用initdb命令。详情请参考initdb。<br />
<br />
在 start 模式里会启动一个新的服务器。服务器是在后台启动的,标准输入被附着到了 /dev/null(在Windows下是nul) 上。在类UNIX系统中,缺省情况下服务器的标准输出和标准错误输出都被发送到pg_ctl's的标准输出(不是标准错误)。pg_ctl的标准输出被重定向到一个文件或者通过管理输送给另一个进行,比如日志处理程序rotatelogs。否则postgres将把它的输出写到控制终端(从后台)并且不会脱离shell进程组。在Windows中,缺省情况下服务器的标准输出和标准错误被发送到终端。这些缺省行为可以通过-l选项来指定log重定向到某个文件。推荐使用-l或者输出重定向。<br />
<br />
在 stop 模式下,那个正在特定数据目录运行的服务器被关闭。你可以用 -m 选项选择三种不同的关闭模式:"Smart" 模式(也是缺省模式)等待所有客户端中断联接且正在进行的备份将被终止。如果服务器不是hot standby,恢复和流复制也将被在客户端连接中断后退出。"Fast" 模式并不等待客户端中断联接。 所有活跃事务都被回滚并且客户端都强制断开。 "Immediate" 模式将在没有干净关闭的情况下退出。这么做将导致在重新启动的时候的恢复。<br />
<br />
restart 实际上是先执行一个停止,然后紧跟一个启动。它允许变换postgres命令行的选项。<br />
<br />
reload 模式简单地给postmaster发送一个 SIGHUP 信号,导致它重新读取她的配置文件 (postgresql.conf,pg_hba.conf 等等) 这样就允许修改配置文件选项而不用完全重启系统来使之生效。<br />
<br />
status 模式检查一个服务器是否在指定的数据目录运行, 如果是,那么显示其 PID 和调用它的命令行选项。<br />
<br />
生产模式,运行在某个数据目录的standby服务器将退出恢复并进开始进行读写操作。<br />
<br />
kill 模式允许你给一个指定的进程发送信号。这个功能对 Microsoft Windows 特别有用,因为那里没有 kill 命令。 使用 --help 查看支持的信号的名字的列表。<br />
<br />
register 模式允许你在 Microsoft Windows 上注册一个系统服务。-S选项用于设定启运类型,或者“自动”(系统启动时自动启动)或者“后台”(后台运行)。<br />
<br />
unregister 模式允许你在 Microsoft Windows 上删除这个系统服务, 这个系统服务是前面用 register 命令注册的。<br />
<br />
'''Options'''<br />
<br />
-c<br />
:允许服务器崩溃时产生core文件,只要其所在的平台允许,所有的信息将会放进core文件中。异常进程产生的堆栈信息对于调试或问题诊断非常有用。<br />
<br />
-D datadir<br />
:声明该数据库文件的文件系统位置。 如果忽略这个选项,使用环境变量 PGDATA。<br />
<br />
-l filename<br />
:把服务器日志输出附加在 filename 文件上。 如果该文件不存在,那么创建它。umask设置为 077, 因此缺省时是不允许从其它用户向日志文件访问的。<br />
<br />
-m mode<br />
:声明关闭模式。mode 可以是smart, fast, 或者 immediate,或者是这三个之一的第一个字母。若不指定,缺省是smart模式。<br />
<br />
-o options<br />
:声明要直接传递给 postgres 的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-o initdb-options<br />
:指定要直接传递给initdb命令行的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-p path<br />
:声明 postgres 可执行文件的位置。缺省时postgres是从和pg_ctl相同的目录取出,如果不是, 那么就是写死的安装目录。除非你想干点什么特别的事情,并且得到类似没有找到postgres这样的错误,否则没有必要使用这个选项。<br />
<br />
:在init模式,该选项相同的用来指定initdb可执行文件的位置。<br />
<br />
-s<br />
:只打印错误,而不打印提示性信息。<br />
<br />
-t<br />
:等待启动或关闭完成的最大秒数。缺省是60秒。<br />
<br />
-w<br />
:等待启动或者关闭的完成。 这个参数是关闭时的缺省值,但不是启动时的缺省值。等待启动时,pg_ctl不断地尝试连接服务器。等待关闭时,pg_ctl等待服务器删除其PID文件。pg_ctl基于启动或关闭成功与否返回退出代码。<br />
<br />
-W<br />
:不等待启动或者停止的完成。这是启动和重起的缺省。<br />
<br />
'''Options for Windows'''<br />
<br />
-N servicename<br />
:要注册的系统服务的名字。这个名字将用于服务名和显示名。<br />
<br />
-P password<br />
:用户启动服务的口令。<br />
<br />
-S start-type<br />
:注册系统服务的启动类型。启动类型可以是auto或demand,或者二者的首字母。如果不指定,缺省值是auto.<br />
<br />
-U username<br />
:启动服务的用户名。对于domain用户,格式为DOMAIN\username.<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
pg_ctl 像其他PostgreSQL实用工具一样,同样使用libpq支持的环境变量(参照Section 31.13)。其他的变量,参照postgres.<br />
<br />
'''Files'''<br />
<br />
postmaster.pid<br />
:该文件是否存在用于帮助pg_ctl判断服务器是否正在运行。<br />
<br />
postmaster.opts<br />
:如果该文件存在于数据目录,pg_ctl(在启动模式下)将以该文件的内容为参数传递给postgres,除非被-o选项覆盖。该文件内容也会在状态模式下显示。<br />
<br />
'''Examples'''<br />
<br />
'''Starting the Server'''<br />
<br />
启动服务器:<br />
<pre>$ pg_ctl start</pre><br />
<br />
启动服务器,等待至服务器接受连接:<br />
<pre>$ pg_ctl -w start</pre><br />
<br />
使用端口5433启动服务器,同时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" start</pre><br />
<br />
'''Stopping the Server'''<br />
<br />
关闭服务器:<br />
<pre>$ pg_ctl stop</pre><br />
<br />
选项-m用来控制服务器关闭方式:<br />
<pre>$ pg_ctl stop -m fast</pre><br />
<br />
'''Restarting the Server'''<br />
<br />
服务器重启几乎等价于关闭后再启动,只是pg_ctl保存并重用了传递给之前进程的命令行选项。最简单的重启:<br />
<pre>$ pg_ctl restart</pre><br />
<br />
重启服务器并等待其关闭并重启:<br />
<pre>$ pg_ctl -w restart</pre><br />
<br />
使用端口5433重启,重启时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" restart</pre><br />
<br />
'''Showing the Server Status'''<br />
<br />
下面是简单的pg_ctl状态输出例子:<br />
<pre><br />
$ pg_ctl status<br />
pg_ctl: server is running (PID: 13718)<br />
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"<br />
</pre><br />
<br />
This is the command line that would be invoked in restart mode.<br />
<br />
'''See Also'''<br />
<br />
initdb, postgres<br />
<br />
== pg_resetxlog ==<br />
<br />
'''Name'''<br />
<br />
重置一个 PostgreSQL 数据库集群的预写日志以及其它控制内容<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_resetxlog [-f] [-n] [-ooid ] [-x xid ] [-e xid_epoch ] [-m mxid ] [-O mxoff ] [-l timelineid,fileid,seg ] datadir</pre><br />
<br />
'''Description'''<br />
<br />
pg_resetxlog 清理预写日志(WAL)并且可以选择地重置其它一些存储在 pg_control 文件中的控制信息。 有时候,如果这些文件崩溃了,我们需要这个功能。 我们一定只把它用作最后的方法,就是说只有因为这样的崩溃导致服务器无法启动的时候才使用。<br />
<br />
在运行这个命令之后,我们可能可以启动服务器了,但是, 一定要记住数据库可能因为部分提交的事务而含有不完整的数据。 你应该马上转储你的数据,运行 initdb,然后重新装载。 在重新装载之后,检查不完整的部分然后根据需要进行修复。<br />
<br />
这个命令只能由安装服务器的用户运行,因为它需要对数据目录的读写权限。 出于安全考虑,你必须在命令行上声明数据目录。 pg_resetxlog 不使用环境变量 PGDATA。<br />
<br />
如果 pg_resetxlog 抱怨说它无法判断用于 pg_control 的有效数据,那么你可以强制它继续处理, 方法是声明 -f (强制)开关。在这种情况下,那些丢失了的数据的值将用模糊的近似数值代替。 大多数字段都可以匹配上,但是下一个 OID,下一个事务 ID,下一个多事务 ID (两阶段提交的东西)和偏移量, WAL 开始地址以及数据库区域字段可能需要手工帮助, 前面几个可以用下面讨论的开关设置。 如果你不能判断所有这些字段的正确数值,那么还是可以使用 -f, 但是这样恢复过来的数据库更要怀疑有问题:立即转储和重置是必须的。 在转储之前不要执行任何修改数据的操作,因为任何这样的动作都可能把事情搞得更糟糕。<br />
<br />
-o,-x,-m,-O 和 -l 开关允许我们手工设置下一个 OID,下一个事务 ID,下一个多事务 ID, 下一个多事务偏移量,以及 WAL 起始位置的数值。只有在 pg_resetxlog 无法通过读取 pg_control 判断合适的数值的时候才需要它。安全的数值可以用下面方法判断:<br />
<br />
:对于下一个事务 ID 而言,一个安全的数值是看看数据目录里的 /pg_clog 里数值最大的文件名, 然后加一,然后再乘上 1048576。 请注意那些文件名是十六进制的。通常我们也以十六进制的形式声明开关值是最简单的。 比如,如果 0011 是 pg_clog 里最大的记录, -x 0x1200000 就可以了(后面的五个零提供了合适的乘积)。<br />
<br />
:下一个多事务 ID (-m)的安全值可以通过查看数据目录里 pg_multixact/offsets 子目录里面的数字最大的文件名,加一,然后乘以 65536 得到。 和上面一样,文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值, 然后在后面加四个零。<br />
<br />
:下一个多事务偏移量(-O)的安全值可以通过检查数目目录里 pg_multixact/members 子目录下的数字最大的文件名,加一,乘以 65536 得到。和上面一样, 文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值,然后背后加四个零。<br />
<br />
:WAL 的起始位置(-l)应该比目前存在于数据目录里得 /pg_xlog 里面的任何文件号都大。它的文件名也是十六进制的,并且有三部分。 第一部分是"时间线 ID",通常应该保持相同。 第三部分不要选择大于 255(0xFF); 应该是在达到 255 的时候给第二部分增一然后重置第三部分为 0。 比如,如果 00000001000000320000004A 是 pg_xlog 里最大的条目, 那么-l 0x1,0x32,0x4B 就可以了; 但如果最大的条目是 000000010000003A000000FF, 选择 -l 0x1,0x3B,0x0 或更多。<br />
<br />
<pre><br />
注意:pg_resetxlog本身看起来在pg_xlog文件里并且选择默认的-l选项来设置为最后存在的文件名。因此,只有明确知道WAL段文件并没有在当前pg_xlog里才可以手动调整-l,比如这些条目在离线的存档里或者pg_xlog内容完全丢失。<br />
</pre><br />
<br />
:我们没有很容易的办法来判断比数据库中最大的 OID 大一号的下一个 OID, 不过很走运的是获取正确的下一个 OID 并非非常关键的事情。<br />
<br />
:事务ID时间轴事实上并不存在于数据库的任何地方,只存在pg_resetxlog设置字段里,所以只要数据库接受都可以工作。你可能需要调整该值来确保复制系统,比如Slony-I,工作正常,这样的话需要从下游复制数据库状态里获得合适的值。<br />
<br />
开关 -n (无操作)指示 pg_resetxlog 打印从 pg_control 重新构造的数值然后不修改任何值就退出。 这主要是一个调试工具,但是在 pg_resetxlog 真正处理前进行的整洁性检查的时候可能会有用。<br />
<br />
-V 和--version选项打印pg_resetxlog版本信息并退出。-?和--help选项输出支持的参数并退出。<br />
<br />
'''Notes'''<br />
<br />
在 postmaster 运行的时候一定不要运行这个命令。 如果发现在数据文件目录里有锁文件,那么 pg_resetxlog 将拒绝启动。如果 postmaster 崩溃,那么可能会剩下一个锁文件; 如果这样,你可以删除该锁文件以便允许 pg_resetxlog 运行。但是在你这么做之前,一定要确信没有任何postmaster或者后端服务器仍在运行。<br />
<br />
== postgres ==<br />
<br />
'''Name'''<br />
<br />
PostgreSQL数据库服务器<br />
<br />
'''Synopsis'''<br />
<br />
<pre>postgres [option...]</pre><br />
<br />
'''Description'''<br />
<br />
postgres 是PostgreSQL数据库服务器。客户端应用为了访问数据库必须连接(通过网络或本地)到一个运行的postgres实例。postgres实例启动一个独立的服务来处理连接。<br />
<br />
一个postgres实例通常管理着整个数据库集群的数据。一个数据库集群是存储在常见文件系统中某个位置(数据区)的数据库集合。同时可以有多个postgres实例运行,只要它们使用不同的数据区以及不同的通信端口(看下面)。postgres需要知道数据区的位置才可以启动。位置必须通过-D选项或者PGDATA环境变量指定,没有缺省值。通常,-D或者PGDATA直接指向initdb创建的数据目录。其他可能的文件布局在18.2章节里讨论过。<br />
<br />
缺省情况下postgres在前台启动并打印日志到标准错误流。实际应用中postgres应该在后台启动,也许是系统启动时。<br />
<br />
postgres命令也可以以单用户模式调用。主要用途是在自举时被initdb调用。有时用于调式或灾难恢复,需要注意的是运行单用户服务器并不真的适合调试服务器,因为没有真实的进程间通信以及锁发生。一旦从shell中进入单用户模式,用户可以进行查询并且查询结果会直接输出到屏幕,但是输出格式对开发者有用但不是终端用户。在单用户模式下,会话用户ID将被设置为1并且隐含的超级用户权限也将授予该用户。该用户事实上不需要退出,所以单用户模式可以用来从某种系统表的灾难损坏中手动恢复。<br />
<br />
'''Options'''<br />
<br />
postgres接受下面的命令行参数。这些参数的详细讨论请查阅18章。如不想每次都敲这些参数,可以将其保存到一个配置文件。一些(安全)选项也可以通过应用特殊的方式设置,这些设置仅针对当前会话。例如,如果设置了PGOPTIONS环境变量,基于libpq的客户端将传递该字符串到服务器,基将被解释成postgres命令行选项。<br />
<br />
'''General Purpose'''<br />
<br />
-A 0|1<br />
:打开运行时断言检查,这是一个帮助发现程序错误的调试手段。该选项只在PosgreSQL被编译阶段开启了断言检查才有效。如打开了,那么断言检查缺省是开启的。<br />
<br />
-B nbuffers<br />
:设置服务进程可以使用的共享缓冲区的大小。initdb自动选定该参数的缺省值。指定该选项等同于设置share_buffers的配置参数。<br />
<br />
-c name=value<br />
:设置一个命名的运行时参数。PostgreSQL 支持的配置参数在 Chapter 18 里描述。 大多数其他命令行选项实际上都是这样的参数赋值的短形式。 -c 可以出现多次用以设置多个参数。<br />
<br />
-d debug-level<br />
:设置调试级别。数值越高,写到服务器日志的调试输出越多。 数值范围是 1 到 5。也可以为特定的会话设置-d 0,以此来阻止postgres父进程输出日志到当前会话。<br />
<br />
-D datadir<br />
:声明数据目录或者配置文件的文件系统路径。细节详见 Section 18.2。<br />
<br />
-e<br />
:把缺省日期风格设置为 "European",也就是说用"DMY"规则解释日期输入, 并且在一些日期输出格式里日子在月份前面打印。 参阅 Section 8.5 获取更多细节。<br />
<br />
-F<br />
:关闭 fsync 调用,提高性能,但是要冒系统崩溃时数据毁坏的风险。 声明这个选项等效关闭了 fsync 参数。在使用之前阅读详细文档!<br />
<br />
-h hostname<br />
:声明监听来自前端应用 TCP/IP 联接的 IP 主机名或地址。 数值也可以是一个用逗号分隔的地址列表,或者*表示监听所有可用的地址。空值表示不监听任何 IP 地址,而只是使用 Unix 域套接字用于与服务器连接。 缺省的时候是只监听 localhost。 声明这个选项等效于设置 listen_addresses 配置参数。<br />
<br />
-i<br />
:这个选项打开 TCP/IP (网际域套接字)通讯。 没有这个选项,只能接受本地连接。 这个选项等效于在 postgresql.conf 中或者通过 -h 设置 listen_addresses 为 *。<br />
<br />
:这个选项已经废弃了,因为它不能实现 listen_addresses 的所有功能。 我们最好直接设置 listen_addresses。<br />
<br />
-k directory<br />
:声明侦听着等待来自前端应用联接的 Unix 域套接字的位置。 缺省通常是 /tmp,但是可以在编包的时候修改。<br />
<br />
-l<br />
:这个选项打开用 SSL 进行的安全通讯。要使用这个选项,编译PostgreSQL时你必须打开了 SSL 选项。 有关使用 SSL 的信息,请参考 Section 17.9。<br />
<br />
-N max-connections<br />
:设置服务器最大可接受客户端的连接数。initdb自动选择缺省值。声明这个选项等效于声明 max_connections 配置参数。<br />
<br />
-o extra-options<br />
:在 extra-options 里声明的 postgres 选项都传递给所有由这个postgres 启动的服务进程。如果选项字串包含任何空格,整个字串必须引起来。<br />
<br />
:该选项已被废弃,所有用于服务进程的命令行选项都可以直接指定。<br />
<br />
-p port<br />
:声明监听着等待客户端应用连接的互联网 TCP/IP 端口或一个本地 Unix 域套接字文件扩展(描述符)。 缺省的端口号是环境变量 PGPORT 的值。如果没有设置 PGPORT,缺省是编译时建立的值(通常是 5432)。 如果你声明了一个非缺省端口,那么所有客户端应用都必须用命令行选项或者 PGPORT 声明同一个端口。<br />
<br />
-s<br />
:在每条命令结束时打印时间信息和其他统计信息。 这个开关对测试性能和调节缓冲区数量有好处。<br />
<br />
-S work-mem<br />
:声明内部排序和散列临时磁盘文件时可以使用的内存数量。 参阅 Section 18.4.1 里描述的配置变量 work_mem。<br />
<br />
--name=value<br />
:设置一个命名的运行时参数;其缩写形式是 -c。<br />
<br />
--describe-config<br />
:这个选项以 tab 分隔的 COPY 格式,倒出服务器内部配置变量,描述和缺省值。 设计它主要是给管理工具使用。<br />
<br />
'''Semi-internal Options'''<br />
<br />
该选项在这里描述主要用于调试目的,在某些场合下帮助恢复严重损坏的数据库。构建产品数据时没理由使用它们。它们列在这只被PostgreSQL系统开发者使用。而且,这些选项在将来的发布中可能不通知地改变或移除。<br />
<br />
-f { s | i | m | n | h }<br />
:禁用特定的扫描和连接方法:s和i分别禁用连续和索引扫描,n、m和h分别禁用嵌入式循环、合并和哈希连接。<br />
<br />
:连续扫描和嵌入式循环都不可能被完全禁止;-fs和-fm选项只是简单地阻止优化器使用该方案。<br />
<br />
-n<br />
:该选项用于调试导致服务器异常挂机问题。这种情况下的策略是通知所有的服务器进程必须中断并且重新初始化共享内存和信号量。因为某个错误的服务器进程可能已经损坏了共享状态。该选项指定postgres不重新初始化共享数据结构。一个有经验的系统程序员可以使用调试器来检查共享内存和信号量状态。<br />
<br />
-O<br />
:允许系统表结构可以被修改。该选项被initdb使用。<br />
<br />
-P<br />
:在读系统表时忽略索引,但是修改这些表时更新索引。这在恢复损坏的系统索引时非常有用。<br />
<br />
-t pa[rser] | pl[anner] | e[xecutor]<br />
:查询主要的系统模块时输出时间统计信息。该选项不能与-s选项一起使用。<br />
<br />
-T<br />
:该选项用于调试导致服务器异常挂机问题。这种情况下的策略是通知所有的服务器进程必须中断并且重新初始化共享内存和信号量。因为某个错误的服务器进程可能已经损坏了共享状态。该选项指定postgres通过发送SIGSTOP来终止所有服务器进程,但是不会导致它们终止。这允许系统程序员手动从服务器进程收集core dumps<br />
<br />
-v protocol<br />
:为特定的会话指定前端和后端使用的协议版本。该选项只做内部使用。<br />
<br />
-W seconds<br />
:设置某个新的服务器进程启动并完成鉴权过程后延迟的秒数。主要提供attach上服务器进程进行debug的机会。<br />
<br />
'''Options for Single-User Mode'''<br />
<br />
下面的选项只提供给单用户模式。<br />
<br />
--single<br />
:选择单用户模式。这必须是命令行选项的第一个参数。<br />
<br />
database<br />
:指定访问的数据库名字。这必须是命令行最后一个参数。如果没有该参数将缺省使用用户名。<br />
<br />
-E<br />
:回显所有的命令。<br />
<br />
-j<br />
:禁用换行符作为语句的分隔符。<br />
<br />
-r filename<br />
:发送所有的服务端日志到某个文件。在正常的多用模式该选项被忽略了,所有进程使用标准错误输出。<br />
<br />
'''Environment'''<br />
<br />
PGCLIENTENCODING<br />
:客户端使用的确缺省字符编码。(客户端可以独立地覆盖这个。)这个值也可以在配置文件里设置。<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
PGDATESTYLE<br />
:运行时参数 DateStyle 的缺省值。(已经不再鼓励使用环境变量了。)<br />
<br />
PGPORT<br />
:缺省端口(最好再配置文件中设置)<br />
<br />
TZ<br />
:服务器时区<br />
<br />
'''Diagnostics'''<br />
<br />
一旦提到了 semget 或者 shmget 的错误信息可能意味着你需要配置你的内核, 提供足够的共享内存和信号量。更多讨论,参阅 Section 17.4。你也可以通过降低 shared_buffers 参数值以减少 PostgreSQL的共享内存的消耗, 或者降低max_connections的参数值减少PostgreSQL的信号量的消耗, 以此可以推迟重新配置内核。<br />
<br />
如果错误信息提示有另外一个服务端已经运行,你就需要仔细检查,比如使用下面的命令:<br />
<br />
<pre>$ ps ax | grep postgres</pre><br />
<br />
or<br />
<br />
<pre>$ ps -ef | grep postgres</pre><br />
<br />
具体使用哪种取决于你的系统。如果确信没有冲突的在运行,那么你可以删除消息里提到的锁文件然后再次运行。<br />
<br />
如果错误信息提示不能绑定某个端口,可以是该端口已被其他非PostgreSQL进程占用。如果你终断了postgres并立即重启且使用了相同的端口也可以遇到这个错误,你需要等几秒终让操作系统关闭这个端口再试。最后,如果你使用了操作系统保留端口也可能遇到错误。比如,很多版本的Unix将端口号小于1024的设为信任并只允许Unix超级用户访问。<br />
<br />
'''Notes'''<br />
<br />
工具命令 pg_ctl 可以用于安全而有效地启停 postgres。<br />
<br />
如果有可能,不要使用 SIGKILL杀死 postgres。 这样会阻止其在退出前释放它持有的系统资源(例如共享内存和信号灯)。这样新的postgres运行时可能会有问题。<br />
<br />
要正常结束 postgres,可以使用信号 SIGTERM,SIGINT, 或 SIGQUIT。 第一个信号将等待所有的客户端退出后才退出。 第二个将强制断开所有客户端,而第三个将不停止立刻退出, 导致在重起时的恢复运行。<br />
<br />
信号SIGHUP将重新加载服务端配置文件。也可以给某个独立的服务进程发SIGHUP,但通常感知不到变化。<br />
<br />
要停止运行一个查询,使用 SIGINT 给正在运行该命令的 postgres 进程发信号。<br />
<br />
postgres服务器使用SIGTERM通知下属的服务进程正常退出,使用SIGQUIT强制退出并不做正常的清理。这些信号不应该被用户使用。给服务进程发送SIGKILL信号是不明智的,postgres主进程将以崩溃终止并且强制让兄弟进程也退出,方便进行崩溃恢复过程。<br />
<br />
'''Bugs'''<br />
<br />
--选项在FreeBSD或者OpenBSD上无效。使用-c替代。这在受影响的操作系统上是个bug;将来的发布如果还不能解决该问题将提供一个修补方案。<br />
<br />
'''Usage'''<br />
<br />
启动一个单用户模式服务器,使用命令<br />
<br />
<pre>postgres --single -D /usr/local/pgsql/data other-options my_database</pre><br />
<br />
用 -D 给服务器提供正确的数据库目录的路径, 或者确保环境变量 PGDATA 已经正确设置。 同时还要声名你想用的特定数据库名字。<br />
<br />
通常,独立运行的服务器把换行符当做命令输入完成字符; 它还不懂分号的作用,因为那些东西是在 psql 里的。 要想把一行分成多行写,你必需在除最后一个换行符以外的每个换行符前面敲一个反斜杠。<br />
<br />
但是如果你使用 -j 命令行开关,那么换行符就不会中止命令输入。 服务器将从标准输入读取数据,直到碰到文件结尾(EOF), 然后把输入当做一个单个的命令字串处理。这个时候反斜杠-换行符就不再当做特殊情况处理。<br />
<br />
要退出会话,键入EOF通常是 Control+D)。 如果你已经使用了 -j,需要用两个连续的EOF来退出。<br />
<br />
请注意独立运行的服务器不会提供复杂的行编辑功能(比如,没有命令行历史)。单用户模式也不做任何后台处理,比如自动检查。<br />
<br />
'''Examples'''<br />
<br />
后台启动postgres并使用缺少参数:<br />
<pre>$ nohup postgres >logfile 2>&1 </dev/null &</pre><br />
<br />
用特定端口,如1234启动postgres:<br />
<pre>$ postgres -p 1234</pre><br />
<br />
使用psql连接到服务端,同时使用-p选项指定端口:<br />
<pre>$ psql -p 1234</pre><br />
<br />
或者也可以设置环境变量PGPORT:<br />
<br />
<pre><br />
$ export PGPORT=1234<br />
$ psql<br />
</pre><br />
<br />
命名的运行时参数可以使用下面的方式之一:<br />
<br />
<pre><br />
$ postgres -c work_mem=1234<br />
$ postgres --work-mem=1234<br />
</pre><br />
<br />
任何一种形式都将覆盖postgresql.conf文件中的work_mem设置。请注意在参数名里的下划线在命令行上可以写成下划线,也可以写成划线。除了短期试验外,推荐使用postgresql.conf而不是打开命令行开关来设置参数。<br />
<br />
'''See Also'''<br />
<br />
initdb, pg_ctl<br />
<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208189.1参考32013-09-20T07:27:31Z<p>Horen: /* postgres */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
<br />
'''Name'''<br />
<br />
pg_ctl -- 初始化、启动、停止和重起 PostgreSQL<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_ctl init[db] [-s] [-D datadir] [-o initdb-options]</pre><br />
<br />
<pre>pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]</pre><br />
<br />
<pre>pg_ctl stop [-W] [-t seconds] [-s] [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ]</pre><br />
<br />
<pre>pg_ctl restart [-w] [-t seconds] [-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o options]</pre><br />
<br />
<pre>pg_ctl reload [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl status [-D datadir]</pre><br />
<br />
<pre>pg_ctl promote [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl kill signal_name process_id</pre><br />
<br />
<pre>pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]</pre><br />
<br />
<pre>pg_ctl unregister [-N servicename]</pre><br />
<br />
'''Description'''<br />
<br />
pg_ctl 是一个用于初始化,启动,停止, 或者重起 PostgreSQL 后端服务器(postgres), 或者显示一个运行着的服务器的状态的工具, 尽管我们可以手动启动服务器,但是 pg_ctl 封装了重新定向日志输出,与终端和进程组合理分离,以及另外提供了方便的选项用于有控制的关闭。<br />
<br />
在Init或initdb模式下创建一个新的PostgreSQL数据库集群。一个数据库集群是由一个服务进程管理的多个数据库集合。该模式调用initdb命令。详情请参考initdb。<br />
<br />
在 start 模式里会启动一个新的服务器。服务器是在后台启动的,标准输入被附着到了 /dev/null(在Windows下是nul) 上。在类UNIX系统中,缺省情况下服务器的标准输出和标准错误输出都被发送到pg_ctl's的标准输出(不是标准错误)。pg_ctl的标准输出被重定向到一个文件或者通过管理输送给另一个进行,比如日志处理程序rotatelogs。否则postgres将把它的输出写到控制终端(从后台)并且不会脱离shell进程组。在Windows中,缺省情况下服务器的标准输出和标准错误被发送到终端。这些缺省行为可以通过-l选项来指定log重定向到某个文件。推荐使用-l或者输出重定向。<br />
<br />
在 stop 模式下,那个正在特定数据目录运行的服务器被关闭。你可以用 -m 选项选择三种不同的关闭模式:"Smart" 模式(也是缺省模式)等待所有客户端中断联接且正在进行的备份将被终止。如果服务器不是hot standby,恢复和流复制也将被在客户端连接中断后退出。"Fast" 模式并不等待客户端中断联接。 所有活跃事务都被回滚并且客户端都强制断开。 "Immediate" 模式将在没有干净关闭的情况下退出。这么做将导致在重新启动的时候的恢复。<br />
<br />
restart 实际上是先执行一个停止,然后紧跟一个启动。它允许变换postgres命令行的选项。<br />
<br />
reload 模式简单地给postmaster发送一个 SIGHUP 信号,导致它重新读取她的配置文件 (postgresql.conf,pg_hba.conf 等等) 这样就允许修改配置文件选项而不用完全重启系统来使之生效。<br />
<br />
status 模式检查一个服务器是否在指定的数据目录运行, 如果是,那么显示其 PID 和调用它的命令行选项。<br />
<br />
生产模式,运行在某个数据目录的standby服务器将退出恢复并进开始进行读写操作。<br />
<br />
kill 模式允许你给一个指定的进程发送信号。这个功能对 Microsoft Windows 特别有用,因为那里没有 kill 命令。 使用 --help 查看支持的信号的名字的列表。<br />
<br />
register 模式允许你在 Microsoft Windows 上注册一个系统服务。-S选项用于设定启运类型,或者“自动”(系统启动时自动启动)或者“后台”(后台运行)。<br />
<br />
unregister 模式允许你在 Microsoft Windows 上删除这个系统服务, 这个系统服务是前面用 register 命令注册的。<br />
<br />
'''Options'''<br />
<br />
-c<br />
:允许服务器崩溃时产生core文件,只要其所在的平台允许,所有的信息将会放进core文件中。异常进程产生的堆栈信息对于调试或问题诊断非常有用。<br />
<br />
-D datadir<br />
:声明该数据库文件的文件系统位置。 如果忽略这个选项,使用环境变量 PGDATA。<br />
<br />
-l filename<br />
:把服务器日志输出附加在 filename 文件上。 如果该文件不存在,那么创建它。umask设置为 077, 因此缺省时是不允许从其它用户向日志文件访问的。<br />
<br />
-m mode<br />
:声明关闭模式。mode 可以是smart, fast, 或者 immediate,或者是这三个之一的第一个字母。若不指定,缺省是smart模式。<br />
<br />
-o options<br />
:声明要直接传递给 postgres 的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-o initdb-options<br />
:指定要直接传递给initdb命令行的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-p path<br />
:声明 postgres 可执行文件的位置。缺省时postgres是从和pg_ctl相同的目录取出,如果不是, 那么就是写死的安装目录。除非你想干点什么特别的事情,并且得到类似没有找到postgres这样的错误,否则没有必要使用这个选项。<br />
<br />
:在init模式,该选项相同的用来指定initdb可执行文件的位置。<br />
<br />
-s<br />
:只打印错误,而不打印提示性信息。<br />
<br />
-t<br />
:等待启动或关闭完成的最大秒数。缺省是60秒。<br />
<br />
-w<br />
:等待启动或者关闭的完成。 这个参数是关闭时的缺省值,但不是启动时的缺省值。等待启动时,pg_ctl不断地尝试连接服务器。等待关闭时,pg_ctl等待服务器删除其PID文件。pg_ctl基于启动或关闭成功与否返回退出代码。<br />
<br />
-W<br />
:不等待启动或者停止的完成。这是启动和重起的缺省。<br />
<br />
'''Options for Windows'''<br />
<br />
-N servicename<br />
:要注册的系统服务的名字。这个名字将用于服务名和显示名。<br />
<br />
-P password<br />
:用户启动服务的口令。<br />
<br />
-S start-type<br />
:注册系统服务的启动类型。启动类型可以是auto或demand,或者二者的首字母。如果不指定,缺省值是auto.<br />
<br />
-U username<br />
:启动服务的用户名。对于domain用户,格式为DOMAIN\username.<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
pg_ctl 像其他PostgreSQL实用工具一样,同样使用libpq支持的环境变量(参照Section 31.13)。其他的变量,参照postgres.<br />
<br />
'''Files'''<br />
<br />
postmaster.pid<br />
:该文件是否存在用于帮助pg_ctl判断服务器是否正在运行。<br />
<br />
postmaster.opts<br />
:如果该文件存在于数据目录,pg_ctl(在启动模式下)将以该文件的内容为参数传递给postgres,除非被-o选项覆盖。该文件内容也会在状态模式下显示。<br />
<br />
'''Examples'''<br />
<br />
'''Starting the Server'''<br />
<br />
启动服务器:<br />
<pre>$ pg_ctl start</pre><br />
<br />
启动服务器,等待至服务器接受连接:<br />
<pre>$ pg_ctl -w start</pre><br />
<br />
使用端口5433启动服务器,同时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" start</pre><br />
<br />
'''Stopping the Server'''<br />
<br />
关闭服务器:<br />
<pre>$ pg_ctl stop</pre><br />
<br />
选项-m用来控制服务器关闭方式:<br />
<pre>$ pg_ctl stop -m fast</pre><br />
<br />
'''Restarting the Server'''<br />
<br />
服务器重启几乎等价于关闭后再启动,只是pg_ctl保存并重用了传递给之前进程的命令行选项。最简单的重启:<br />
<pre>$ pg_ctl restart</pre><br />
<br />
重启服务器并等待其关闭并重启:<br />
<pre>$ pg_ctl -w restart</pre><br />
<br />
使用端口5433重启,重启时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" restart</pre><br />
<br />
'''Showing the Server Status'''<br />
<br />
下面是简单的pg_ctl状态输出例子:<br />
<pre><br />
$ pg_ctl status<br />
pg_ctl: server is running (PID: 13718)<br />
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"<br />
</pre><br />
<br />
This is the command line that would be invoked in restart mode.<br />
<br />
'''See Also'''<br />
<br />
initdb, postgres<br />
<br />
== pg_resetxlog ==<br />
<br />
'''Name'''<br />
<br />
重置一个 PostgreSQL 数据库集群的预写日志以及其它控制内容<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_resetxlog [-f] [-n] [-ooid ] [-x xid ] [-e xid_epoch ] [-m mxid ] [-O mxoff ] [-l timelineid,fileid,seg ] datadir</pre><br />
<br />
'''Description'''<br />
<br />
pg_resetxlog 清理预写日志(WAL)并且可以选择地重置其它一些存储在 pg_control 文件中的控制信息。 有时候,如果这些文件崩溃了,我们需要这个功能。 我们一定只把它用作最后的方法,就是说只有因为这样的崩溃导致服务器无法启动的时候才使用。<br />
<br />
在运行这个命令之后,我们可能可以启动服务器了,但是, 一定要记住数据库可能因为部分提交的事务而含有不完整的数据。 你应该马上转储你的数据,运行 initdb,然后重新装载。 在重新装载之后,检查不完整的部分然后根据需要进行修复。<br />
<br />
这个命令只能由安装服务器的用户运行,因为它需要对数据目录的读写权限。 出于安全考虑,你必须在命令行上声明数据目录。 pg_resetxlog 不使用环境变量 PGDATA。<br />
<br />
如果 pg_resetxlog 抱怨说它无法判断用于 pg_control 的有效数据,那么你可以强制它继续处理, 方法是声明 -f (强制)开关。在这种情况下,那些丢失了的数据的值将用模糊的近似数值代替。 大多数字段都可以匹配上,但是下一个 OID,下一个事务 ID,下一个多事务 ID (两阶段提交的东西)和偏移量, WAL 开始地址以及数据库区域字段可能需要手工帮助, 前面几个可以用下面讨论的开关设置。 如果你不能判断所有这些字段的正确数值,那么还是可以使用 -f, 但是这样恢复过来的数据库更要怀疑有问题:立即转储和重置是必须的。 在转储之前不要执行任何修改数据的操作,因为任何这样的动作都可能把事情搞得更糟糕。<br />
<br />
-o,-x,-m,-O 和 -l 开关允许我们手工设置下一个 OID,下一个事务 ID,下一个多事务 ID, 下一个多事务偏移量,以及 WAL 起始位置的数值。只有在 pg_resetxlog 无法通过读取 pg_control 判断合适的数值的时候才需要它。安全的数值可以用下面方法判断:<br />
<br />
:对于下一个事务 ID 而言,一个安全的数值是看看数据目录里的 /pg_clog 里数值最大的文件名, 然后加一,然后再乘上 1048576。 请注意那些文件名是十六进制的。通常我们也以十六进制的形式声明开关值是最简单的。 比如,如果 0011 是 pg_clog 里最大的记录, -x 0x1200000 就可以了(后面的五个零提供了合适的乘积)。<br />
<br />
:下一个多事务 ID (-m)的安全值可以通过查看数据目录里 pg_multixact/offsets 子目录里面的数字最大的文件名,加一,然后乘以 65536 得到。 和上面一样,文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值, 然后在后面加四个零。<br />
<br />
:下一个多事务偏移量(-O)的安全值可以通过检查数目目录里 pg_multixact/members 子目录下的数字最大的文件名,加一,乘以 65536 得到。和上面一样, 文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值,然后背后加四个零。<br />
<br />
:WAL 的起始位置(-l)应该比目前存在于数据目录里得 /pg_xlog 里面的任何文件号都大。它的文件名也是十六进制的,并且有三部分。 第一部分是"时间线 ID",通常应该保持相同。 第三部分不要选择大于 255(0xFF); 应该是在达到 255 的时候给第二部分增一然后重置第三部分为 0。 比如,如果 00000001000000320000004A 是 pg_xlog 里最大的条目, 那么-l 0x1,0x32,0x4B 就可以了; 但如果最大的条目是 000000010000003A000000FF, 选择 -l 0x1,0x3B,0x0 或更多。<br />
<br />
<pre><br />
注意:pg_resetxlog本身看起来在pg_xlog文件里并且选择默认的-l选项来设置为最后存在的文件名。因此,只有明确知道WAL段文件并没有在当前pg_xlog里才可以手动调整-l,比如这些条目在离线的存档里或者pg_xlog内容完全丢失。<br />
</pre><br />
<br />
:我们没有很容易的办法来判断比数据库中最大的 OID 大一号的下一个 OID, 不过很走运的是获取正确的下一个 OID 并非非常关键的事情。<br />
<br />
:事务ID时间轴事实上并不存在于数据库的任何地方,只存在pg_resetxlog设置字段里,所以只要数据库接受都可以工作。你可能需要调整该值来确保复制系统,比如Slony-I,工作正常,这样的话需要从下游复制数据库状态里获得合适的值。<br />
<br />
开关 -n (无操作)指示 pg_resetxlog 打印从 pg_control 重新构造的数值然后不修改任何值就退出。 这主要是一个调试工具,但是在 pg_resetxlog 真正处理前进行的整洁性检查的时候可能会有用。<br />
<br />
-V 和--version选项打印pg_resetxlog版本信息并退出。-?和--help选项输出支持的参数并退出。<br />
<br />
'''Notes'''<br />
<br />
在 postmaster 运行的时候一定不要运行这个命令。 如果发现在数据文件目录里有锁文件,那么 pg_resetxlog 将拒绝启动。如果 postmaster 崩溃,那么可能会剩下一个锁文件; 如果这样,你可以删除该锁文件以便允许 pg_resetxlog 运行。但是在你这么做之前,一定要确信没有任何postmaster或者后端服务器仍在运行。<br />
<br />
== postgres ==<br />
<br />
'''Name'''<br />
<br />
postgres -- PostgreSQL database server<br />
<br />
'''Synopsis'''<br />
<br />
<pre>postgres [option...]</pre><br />
<br />
'''Description'''<br />
<br />
postgres is the PostgreSQL database server. In order for a client application to access a database it connects (over a network or locally) to a running postgres instance. The postgres instance then starts a separate server process to handle the connection.<br />
<br />
One postgres instance always manages the data of exactly one database cluster. A database cluster is a collection of databases that is stored at a common file system location (the "data area"). More than one postgres instance can run on a system at one time, so long as they use different data areas and different communication ports (see below). When postgres starts it needs to know the location of the data area. The location must be specified by the -D option or the PGDATA environment variable; there is no default. Typically, -D or PGDATA points directly to the data area directory created by initdb. Other possible file layouts are discussed in Section 18.2.<br />
<br />
By default postgres starts in the foreground and prints log messages to the standard error stream. In practical applications postgres should be started as a background process, perhaps at boot time.<br />
<br />
The postgres command can also be called in single-user mode. The primary use for this mode is during bootstrapping by initdb. Sometimes it is used for debugging or disaster recovery; note that running a single-user server is not truly suitable for debugging the server, since no realistic interprocess communication and locking will happen. When invoked in single-user mode from the shell, the user can enter queries and the results will be printed to the screen, but in a form that is more useful for developers than end users. In the single-user mode, the session user will be set to the user with ID 1, and implicit superuser powers are granted to this user. This user does not actually have to exist, so the single-user mode can be used to manually recover from certain kinds of accidental damage to the system catalogs.<br />
<br />
'''Options'''<br />
<br />
postgres accepts the following command-line arguments. For a detailed discussion of the options consult Chapter 18. You can save typing most of these options by setting up a configuration file. Some (safe) options can also be set from the connecting client in an application-dependent way to apply only for that session. For example, if the environment variable PGOPTIONS is set, then libpq-based clients will pass that string to the server, which will interpret it as postgres command-line options.<br />
<br />
'''General Purpose'''<br />
<br />
-A 0|1<br />
:Enables run-time assertion checks, which is a debugging aid to detect programming mistakes. This option is only available if assertions were enabled when PostgreSQL was compiled. If so, the default is on.<br />
<br />
-B nbuffers<br />
:Sets the number of shared buffers for use by the server processes. The default value of this parameter is chosen automatically by initdb. Specifying this option is equivalent to setting the shared_buffers configuration parameter.<br />
<br />
-c name=value<br />
:Sets a named run-time parameter. The configuration parameters supported by PostgreSQL are described in Chapter 18. Most of the other command line options are in fact short forms of such a parameter assignment. -c can appear multiple times to set multiple parameters.<br />
<br />
-d debug-level<br />
:Sets the debug level. The higher this value is set, the more debugging output is written to the server log. Values are from 1 to 5. It is also possible to pass -d 0 for a specific session, which will prevent the server log level of the parent postgres process from being propagated to this session.<br />
<br />
-D datadir<br />
:Specifies the file system location of the data directory or configuration file(s). See Section 18.2 for details.<br />
<br />
-e<br />
:Sets the default date style to "European", that is DMY ordering of input date fields. This also causes the day to be printed before the month in certain date output formats. See Section 8.5 for more information.<br />
<br />
-F<br />
:Disables fsync calls for improved performance, at the risk of data corruption in the event of a system crash. Specifying this option is equivalent to disabling the fsync configuration parameter. Read the detailed documentation before using this!<br />
<br />
-h hostname<br />
:Specifies the IP host name or address on which postgres is to listen for TCP/IP connections from client applications. The value can also be a comma-separated list of addresses, or * to specify listening on all available interfaces. An empty value specifies not listening on any IP addresses, in which case only Unix-domain sockets can be used to connect to the server. Defaults to listening only on localhost. Specifying this option is equivalent to setting the listen_addresses configuration parameter.<br />
<br />
-i<br />
:Allows remote clients to connect via TCP/IP (Internet domain) connections. Without this option, only local connections are accepted. This option is equivalent to setting listen_addresses to * in postgresql.conf or via -h.<br />
<br />
:This option is deprecated since it does not allow access to the full functionality of listen_addresses. It's usually better to set listen_addresses directly.<br />
<br />
-k directory<br />
:Specifies the directory of the Unix-domain socket on which postgres is to listen for connections from client applications. The default is normally /tmp, but can be changed at build time.<br />
<br />
-l<br />
:Enables secure connections using SSL. PostgreSQL must have been compiled with support for SSL for this option to be available. For more information on using SSL, refer to Section 17.9.<br />
<br />
-N max-connections<br />
:Sets the maximum number of client connections that this server will accept. The default value of this parameter is chosen automatically by initdb. Specifying this option is equivalent to setting the max_connections configuration parameter.<br />
<br />
-o extra-options<br />
:The command-line-style options specified in extra-options are passed to all server processes started by this postgres process. If the option string contains any spaces, the entire string must be quoted.<br />
<br />
:The use of this option is obsolete; all command-line options for server processes can be specified directly on the postgres command line.<br />
<br />
-p port<br />
:Specifies the TCP/IP port or local Unix domain socket file extension on which postgres is to listen for connections from client applications. Defaults to the value of the PGPORT environment variable, or if PGPORT is not set, then defaults to the value established during compilation (normally 5432). If you specify a port other than the default port, then all client applications must specify the same port using either command-line options or PGPORT.<br />
<br />
-s<br />
:Print time information and other statistics at the end of each command. This is useful for benchmarking or for use in tuning the number of buffers.<br />
<br />
-S work-mem<br />
:Specifies the amount of memory to be used by internal sorts and hashes before resorting to temporary disk files. See the description of the work_mem configuration parameter in Section 18.4.1.<br />
<br />
--name=value<br />
:Sets a named run-time parameter; a shorter form of -c.<br />
<br />
--describe-config<br />
:This option dumps out the server's internal configuration variables, descriptions, and defaults in tab-delimited COPY format. It is designed primarily for use by administration tools.<br />
<br />
'''Semi-internal Options'''<br />
<br />
The options described here are used mainly for debugging purposes, and in some cases to assist with recovery of severely damaged databases. There should be no reason to use them in a production database setup. They are listed here only for use by PostgreSQL system developers. Furthermore, these options might change or be removed in a future release without notice.<br />
<br />
-f { s | i | m | n | h }<br />
:Forbids the use of particular scan and join methods: s and i disable sequential and index scans respectively, while n, m, and h disable nested-loop, merge and hash joins respectively.<br />
<br />
:Neither sequential scans nor nested-loop joins can be disabled completely; the -fs and -fn options simply discourage the optimizer from using those plan types if it has any other alternative.<br />
<br />
-n<br />
:This option is for debugging problems that cause a server process to die abnormally. The ordinary strategy in this situation is to notify all other server processes that they must terminate and then reinitialize the shared memory and semaphores. This is because an errant server process could have corrupted some shared state before terminating. This option specifies that postgres will not reinitialize shared data structures. A knowledgeable system programmer can then use a debugger to examine shared memory and semaphore state.<br />
<br />
-O<br />
:Allows the structure of system tables to be modified. This is used by initdb.<br />
<br />
-P<br />
:Ignore system indexes when reading system tables, but still update the indexes when modifying the tables. This is useful when recovering from damaged system indexes.<br />
<br />
-t pa[rser] | pl[anner] | e[xecutor]<br />
:Print timing statistics for each query relating to each of the major system modules. This option cannot be used together with the -s option.<br />
<br />
-T<br />
:This option is for debugging problems that cause a server process to die abnormally. The ordinary strategy in this situation is to notify all other server processes that they must terminate and then reinitialize the shared memory and semaphores. This is because an errant server process could have corrupted some shared state before terminating. This option specifies that postgres will stop all other server processes by sending the signal SIGSTOP, but will not cause them to terminate. This permits system programmers to collect core dumps from all server processes by hand.<br />
<br />
-v protocol<br />
:Specifies the version number of the frontend/backend protocol to be used for a particular session. This option is for internal use only.<br />
<br />
-W seconds<br />
:A delay of this many seconds occurs when a new server process is started, after it conducts the authentication procedure. This is intended to give an opportunity to attach to the server process with a debugger.<br />
<br />
'''Options for Single-User Mode'''<br />
<br />
The following options only apply to the single-user mode.<br />
<br />
--single<br />
:Selects the single-user mode. This must be the first argument on the command line.<br />
<br />
database<br />
:Specifies the name of the database to be accessed. This must be the last argument on the command line. If it is omitted it defaults to the user name.<br />
<br />
-E<br />
:Echo all commands.<br />
<br />
-j<br />
:Disables use of newline as a statement delimiter.<br />
<br />
-r filename<br />
:Send all server log output to filename. In normal multiuser mode, this option is ignored, and stderr is used by all processes.<br />
<br />
'''Environment'''<br />
<br />
PGCLIENTENCODING<br />
:Default character encoding used by clients. (The clients can override this individually.) This value can also be set in the configuration file.<br />
<br />
PGDATA<br />
:Default data directory location<br />
<br />
PGDATESTYLE<br />
:Default value of the DateStyle run-time parameter. (The use of this environment variable is deprecated.)<br />
<br />
PGPORT<br />
:Default port number (preferably set in the configuration file)<br />
<br />
TZ<br />
:Server time zone<br />
<br />
'''Diagnostics'''<br />
<br />
A failure message mentioning semget or shmget probably indicates you need to configure your kernel to provide adequate shared memory and semaphores. For more discussion see Section 17.4. You might be able to postpone reconfiguring your kernel by decreasing shared_buffers to reduce the shared memory consumption of PostgreSQL, and/or by reducing max_connections to reduce the semaphore consumption.<br />
<br />
A failure message suggesting that another server is already running should be checked carefully, for example by using the command<br />
<br />
<pre>$ ps ax | grep postgres</pre><br />
<br />
or<br />
<br />
<pre>$ ps -ef | grep postgres</pre><br />
<br />
depending on your system. If you are certain that no conflicting server is running, you can remove the lock file mentioned in the message and try again.<br />
<br />
A failure message indicating inability to bind to a port might indicate that that port is already in use by some non-PostgreSQL process. You might also get this error if you terminate postgres and immediately restart it using the same port; in this case, you must simply wait a few seconds until the operating system closes the port before trying again. Finally, you might get this error if you specify a port number that your operating system considers to be reserved. For example, many versions of Unix consider port numbers under 1024 to be "trusted" and only permit the Unix superuser to access them.<br />
<br />
'''Notes'''<br />
<br />
The utility command pg_ctl can be used to start and shut down the postgres server safely and comfortably.<br />
<br />
If at all possible, do not use SIGKILL to kill the main postgres server. Doing so will prevent postgres from freeing the system resources (e.g., shared memory and semaphores) that it holds before terminating. This might cause problems for starting a fresh postgres run.<br />
<br />
To terminate the postgres server normally, the signals SIGTERM, SIGINT, or SIGQUIT can be used. The first will wait for all clients to terminate before quitting, the second will forcefully disconnect all clients, and the third will quit immediately without proper shutdown, resulting in a recovery run during restart.<br />
<br />
The SIGHUP signal will reload the server configuration files. It is also possible to send SIGHUP to an individual server process, but that is usually not sensible.<br />
<br />
To cancel a running query, send the SIGINT signal to the process running that command.<br />
<br />
The postgres server uses SIGTERM to tell subordinate server processes to quit normally and SIGQUIT to terminate without the normal cleanup. These signals should not be used by users. It is also unwise to send SIGKILL to a server process — the main postgres process will interpret this as a crash and will force all the sibling processes to quit as part of its standard crash-recovery procedure.<br />
<br />
'''Bugs'''<br />
<br />
The -- options will not work on FreeBSD or OpenBSD. Use -c instead. This is a bug in the affected operating systems; a future release of PostgreSQL will provide a workaround if this is not fixed.<br />
<br />
'''Usage'''<br />
<br />
To start a single-user mode server, use a command like<br />
<br />
<pre>postgres --single -D /usr/local/pgsql/data other-options my_database</pre><br />
<br />
Provide the correct path to the database directory with -D, or make sure that the environment variable PGDATA is set. Also specify the name of the particular database you want to work in.<br />
<br />
Normally, the single-user mode server treats newline as the command entry terminator; there is no intelligence about semicolons, as there is in psql. To continue a command across multiple lines, you must type backslash just before each newline except the last one.<br />
<br />
But if you use the -j command line switch, then newline does not terminate command entry. In this case, the server will read the standard input until the end-of-file (EOF) marker, then process the input as a single command string. Backslash-newline is not treated specially in this case.<br />
<br />
To quit the session, type EOF (Control+D, usually). If you've used -j, two consecutive EOFs are needed to exit.<br />
<br />
Note that the single-user mode server does not provide sophisticated line-editing features (no command history, for example). Single-User mode also does not do any background processing, like automatic checkpoints.<br />
<br />
'''Examples'''<br />
<br />
To start postgres in the background using default values, type:<br />
<pre>$ nohup postgres >logfile 2>&1 </dev/null &</pre><br />
<br />
To start postgres with a specific port, e.g. 1234:<br />
<pre>$ postgres -p 1234</pre><br />
<br />
To connect to this server using psql, specify this port with the -p option:<br />
<pre>$ psql -p 1234</pre><br />
<br />
or set the environment variable PGPORT:<br />
<br />
<pre><br />
$ export PGPORT=1234<br />
$ psql<br />
</pre><br />
<br />
Named run-time parameters can be set in either of these styles:<br />
<br />
<pre><br />
$ postgres -c work_mem=1234<br />
$ postgres --work-mem=1234<br />
</pre><br />
<br />
Either form overrides whatever setting might exist for work_mem in postgresql.conf. Notice that underscores in parameter names can be written as either underscore or dash on the command line. Except for short-term experiments, it's probably better practice to edit the setting in postgresql.conf than to rely on a command-line switch to set a parameter.<br />
<br />
'''See Also'''<br />
<br />
initdb, pg_ctl<br />
<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208179.1参考32013-09-20T07:13:38Z<p>Horen: /* pg_resetxlog */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
<br />
'''Name'''<br />
<br />
pg_ctl -- 初始化、启动、停止和重起 PostgreSQL<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_ctl init[db] [-s] [-D datadir] [-o initdb-options]</pre><br />
<br />
<pre>pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]</pre><br />
<br />
<pre>pg_ctl stop [-W] [-t seconds] [-s] [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ]</pre><br />
<br />
<pre>pg_ctl restart [-w] [-t seconds] [-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o options]</pre><br />
<br />
<pre>pg_ctl reload [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl status [-D datadir]</pre><br />
<br />
<pre>pg_ctl promote [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl kill signal_name process_id</pre><br />
<br />
<pre>pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]</pre><br />
<br />
<pre>pg_ctl unregister [-N servicename]</pre><br />
<br />
'''Description'''<br />
<br />
pg_ctl 是一个用于初始化,启动,停止, 或者重起 PostgreSQL 后端服务器(postgres), 或者显示一个运行着的服务器的状态的工具, 尽管我们可以手动启动服务器,但是 pg_ctl 封装了重新定向日志输出,与终端和进程组合理分离,以及另外提供了方便的选项用于有控制的关闭。<br />
<br />
在Init或initdb模式下创建一个新的PostgreSQL数据库集群。一个数据库集群是由一个服务进程管理的多个数据库集合。该模式调用initdb命令。详情请参考initdb。<br />
<br />
在 start 模式里会启动一个新的服务器。服务器是在后台启动的,标准输入被附着到了 /dev/null(在Windows下是nul) 上。在类UNIX系统中,缺省情况下服务器的标准输出和标准错误输出都被发送到pg_ctl's的标准输出(不是标准错误)。pg_ctl的标准输出被重定向到一个文件或者通过管理输送给另一个进行,比如日志处理程序rotatelogs。否则postgres将把它的输出写到控制终端(从后台)并且不会脱离shell进程组。在Windows中,缺省情况下服务器的标准输出和标准错误被发送到终端。这些缺省行为可以通过-l选项来指定log重定向到某个文件。推荐使用-l或者输出重定向。<br />
<br />
在 stop 模式下,那个正在特定数据目录运行的服务器被关闭。你可以用 -m 选项选择三种不同的关闭模式:"Smart" 模式(也是缺省模式)等待所有客户端中断联接且正在进行的备份将被终止。如果服务器不是hot standby,恢复和流复制也将被在客户端连接中断后退出。"Fast" 模式并不等待客户端中断联接。 所有活跃事务都被回滚并且客户端都强制断开。 "Immediate" 模式将在没有干净关闭的情况下退出。这么做将导致在重新启动的时候的恢复。<br />
<br />
restart 实际上是先执行一个停止,然后紧跟一个启动。它允许变换postgres命令行的选项。<br />
<br />
reload 模式简单地给postmaster发送一个 SIGHUP 信号,导致它重新读取她的配置文件 (postgresql.conf,pg_hba.conf 等等) 这样就允许修改配置文件选项而不用完全重启系统来使之生效。<br />
<br />
status 模式检查一个服务器是否在指定的数据目录运行, 如果是,那么显示其 PID 和调用它的命令行选项。<br />
<br />
生产模式,运行在某个数据目录的standby服务器将退出恢复并进开始进行读写操作。<br />
<br />
kill 模式允许你给一个指定的进程发送信号。这个功能对 Microsoft Windows 特别有用,因为那里没有 kill 命令。 使用 --help 查看支持的信号的名字的列表。<br />
<br />
register 模式允许你在 Microsoft Windows 上注册一个系统服务。-S选项用于设定启运类型,或者“自动”(系统启动时自动启动)或者“后台”(后台运行)。<br />
<br />
unregister 模式允许你在 Microsoft Windows 上删除这个系统服务, 这个系统服务是前面用 register 命令注册的。<br />
<br />
'''Options'''<br />
<br />
-c<br />
:允许服务器崩溃时产生core文件,只要其所在的平台允许,所有的信息将会放进core文件中。异常进程产生的堆栈信息对于调试或问题诊断非常有用。<br />
<br />
-D datadir<br />
:声明该数据库文件的文件系统位置。 如果忽略这个选项,使用环境变量 PGDATA。<br />
<br />
-l filename<br />
:把服务器日志输出附加在 filename 文件上。 如果该文件不存在,那么创建它。umask设置为 077, 因此缺省时是不允许从其它用户向日志文件访问的。<br />
<br />
-m mode<br />
:声明关闭模式。mode 可以是smart, fast, 或者 immediate,或者是这三个之一的第一个字母。若不指定,缺省是smart模式。<br />
<br />
-o options<br />
:声明要直接传递给 postgres 的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-o initdb-options<br />
:指定要直接传递给initdb命令行的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-p path<br />
:声明 postgres 可执行文件的位置。缺省时postgres是从和pg_ctl相同的目录取出,如果不是, 那么就是写死的安装目录。除非你想干点什么特别的事情,并且得到类似没有找到postgres这样的错误,否则没有必要使用这个选项。<br />
<br />
:在init模式,该选项相同的用来指定initdb可执行文件的位置。<br />
<br />
-s<br />
:只打印错误,而不打印提示性信息。<br />
<br />
-t<br />
:等待启动或关闭完成的最大秒数。缺省是60秒。<br />
<br />
-w<br />
:等待启动或者关闭的完成。 这个参数是关闭时的缺省值,但不是启动时的缺省值。等待启动时,pg_ctl不断地尝试连接服务器。等待关闭时,pg_ctl等待服务器删除其PID文件。pg_ctl基于启动或关闭成功与否返回退出代码。<br />
<br />
-W<br />
:不等待启动或者停止的完成。这是启动和重起的缺省。<br />
<br />
'''Options for Windows'''<br />
<br />
-N servicename<br />
:要注册的系统服务的名字。这个名字将用于服务名和显示名。<br />
<br />
-P password<br />
:用户启动服务的口令。<br />
<br />
-S start-type<br />
:注册系统服务的启动类型。启动类型可以是auto或demand,或者二者的首字母。如果不指定,缺省值是auto.<br />
<br />
-U username<br />
:启动服务的用户名。对于domain用户,格式为DOMAIN\username.<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
pg_ctl 像其他PostgreSQL实用工具一样,同样使用libpq支持的环境变量(参照Section 31.13)。其他的变量,参照postgres.<br />
<br />
'''Files'''<br />
<br />
postmaster.pid<br />
:该文件是否存在用于帮助pg_ctl判断服务器是否正在运行。<br />
<br />
postmaster.opts<br />
:如果该文件存在于数据目录,pg_ctl(在启动模式下)将以该文件的内容为参数传递给postgres,除非被-o选项覆盖。该文件内容也会在状态模式下显示。<br />
<br />
'''Examples'''<br />
<br />
'''Starting the Server'''<br />
<br />
启动服务器:<br />
<pre>$ pg_ctl start</pre><br />
<br />
启动服务器,等待至服务器接受连接:<br />
<pre>$ pg_ctl -w start</pre><br />
<br />
使用端口5433启动服务器,同时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" start</pre><br />
<br />
'''Stopping the Server'''<br />
<br />
关闭服务器:<br />
<pre>$ pg_ctl stop</pre><br />
<br />
选项-m用来控制服务器关闭方式:<br />
<pre>$ pg_ctl stop -m fast</pre><br />
<br />
'''Restarting the Server'''<br />
<br />
服务器重启几乎等价于关闭后再启动,只是pg_ctl保存并重用了传递给之前进程的命令行选项。最简单的重启:<br />
<pre>$ pg_ctl restart</pre><br />
<br />
重启服务器并等待其关闭并重启:<br />
<pre>$ pg_ctl -w restart</pre><br />
<br />
使用端口5433重启,重启时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" restart</pre><br />
<br />
'''Showing the Server Status'''<br />
<br />
下面是简单的pg_ctl状态输出例子:<br />
<pre><br />
$ pg_ctl status<br />
pg_ctl: server is running (PID: 13718)<br />
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"<br />
</pre><br />
<br />
This is the command line that would be invoked in restart mode.<br />
<br />
'''See Also'''<br />
<br />
initdb, postgres<br />
<br />
== pg_resetxlog ==<br />
<br />
'''Name'''<br />
<br />
重置一个 PostgreSQL 数据库集群的预写日志以及其它控制内容<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_resetxlog [-f] [-n] [-ooid ] [-x xid ] [-e xid_epoch ] [-m mxid ] [-O mxoff ] [-l timelineid,fileid,seg ] datadir</pre><br />
<br />
'''Description'''<br />
<br />
pg_resetxlog 清理预写日志(WAL)并且可以选择地重置其它一些存储在 pg_control 文件中的控制信息。 有时候,如果这些文件崩溃了,我们需要这个功能。 我们一定只把它用作最后的方法,就是说只有因为这样的崩溃导致服务器无法启动的时候才使用。<br />
<br />
在运行这个命令之后,我们可能可以启动服务器了,但是, 一定要记住数据库可能因为部分提交的事务而含有不完整的数据。 你应该马上转储你的数据,运行 initdb,然后重新装载。 在重新装载之后,检查不完整的部分然后根据需要进行修复。<br />
<br />
这个命令只能由安装服务器的用户运行,因为它需要对数据目录的读写权限。 出于安全考虑,你必须在命令行上声明数据目录。 pg_resetxlog 不使用环境变量 PGDATA。<br />
<br />
如果 pg_resetxlog 抱怨说它无法判断用于 pg_control 的有效数据,那么你可以强制它继续处理, 方法是声明 -f (强制)开关。在这种情况下,那些丢失了的数据的值将用模糊的近似数值代替。 大多数字段都可以匹配上,但是下一个 OID,下一个事务 ID,下一个多事务 ID (两阶段提交的东西)和偏移量, WAL 开始地址以及数据库区域字段可能需要手工帮助, 前面几个可以用下面讨论的开关设置。 如果你不能判断所有这些字段的正确数值,那么还是可以使用 -f, 但是这样恢复过来的数据库更要怀疑有问题:立即转储和重置是必须的。 在转储之前不要执行任何修改数据的操作,因为任何这样的动作都可能把事情搞得更糟糕。<br />
<br />
-o,-x,-m,-O 和 -l 开关允许我们手工设置下一个 OID,下一个事务 ID,下一个多事务 ID, 下一个多事务偏移量,以及 WAL 起始位置的数值。只有在 pg_resetxlog 无法通过读取 pg_control 判断合适的数值的时候才需要它。安全的数值可以用下面方法判断:<br />
<br />
:对于下一个事务 ID 而言,一个安全的数值是看看数据目录里的 /pg_clog 里数值最大的文件名, 然后加一,然后再乘上 1048576。 请注意那些文件名是十六进制的。通常我们也以十六进制的形式声明开关值是最简单的。 比如,如果 0011 是 pg_clog 里最大的记录, -x 0x1200000 就可以了(后面的五个零提供了合适的乘积)。<br />
<br />
:下一个多事务 ID (-m)的安全值可以通过查看数据目录里 pg_multixact/offsets 子目录里面的数字最大的文件名,加一,然后乘以 65536 得到。 和上面一样,文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值, 然后在后面加四个零。<br />
<br />
:下一个多事务偏移量(-O)的安全值可以通过检查数目目录里 pg_multixact/members 子目录下的数字最大的文件名,加一,乘以 65536 得到。和上面一样, 文件名是十六进制的,因此最简单的方法是给开关声明一个十六进制的开关值,然后背后加四个零。<br />
<br />
:WAL 的起始位置(-l)应该比目前存在于数据目录里得 /pg_xlog 里面的任何文件号都大。它的文件名也是十六进制的,并且有三部分。 第一部分是"时间线 ID",通常应该保持相同。 第三部分不要选择大于 255(0xFF); 应该是在达到 255 的时候给第二部分增一然后重置第三部分为 0。 比如,如果 00000001000000320000004A 是 pg_xlog 里最大的条目, 那么-l 0x1,0x32,0x4B 就可以了; 但如果最大的条目是 000000010000003A000000FF, 选择 -l 0x1,0x3B,0x0 或更多。<br />
<br />
<pre><br />
注意:pg_resetxlog本身看起来在pg_xlog文件里并且选择默认的-l选项来设置为最后存在的文件名。因此,只有明确知道WAL段文件并没有在当前pg_xlog里才可以手动调整-l,比如这些条目在离线的存档里或者pg_xlog内容完全丢失。<br />
</pre><br />
<br />
:我们没有很容易的办法来判断比数据库中最大的 OID 大一号的下一个 OID, 不过很走运的是获取正确的下一个 OID 并非非常关键的事情。<br />
<br />
:事务ID时间轴事实上并不存在于数据库的任何地方,只存在pg_resetxlog设置字段里,所以只要数据库接受都可以工作。你可能需要调整该值来确保复制系统,比如Slony-I,工作正常,这样的话需要从下游复制数据库状态里获得合适的值。<br />
<br />
开关 -n (无操作)指示 pg_resetxlog 打印从 pg_control 重新构造的数值然后不修改任何值就退出。 这主要是一个调试工具,但是在 pg_resetxlog 真正处理前进行的整洁性检查的时候可能会有用。<br />
<br />
-V 和--version选项打印pg_resetxlog版本信息并退出。-?和--help选项输出支持的参数并退出。<br />
<br />
'''Notes'''<br />
<br />
在 postmaster 运行的时候一定不要运行这个命令。 如果发现在数据文件目录里有锁文件,那么 pg_resetxlog 将拒绝启动。如果 postmaster 崩溃,那么可能会剩下一个锁文件; 如果这样,你可以删除该锁文件以便允许 pg_resetxlog 运行。但是在你这么做之前,一定要确信没有任何postmaster或者后端服务器仍在运行。<br />
<br />
== postgres ==<br />
-- PostgreSQL database server<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208169.1参考32013-09-20T07:06:54Z<p>Horen: /* pg_resetxlog */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
<br />
'''Name'''<br />
<br />
pg_ctl -- 初始化、启动、停止和重起 PostgreSQL<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_ctl init[db] [-s] [-D datadir] [-o initdb-options]</pre><br />
<br />
<pre>pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]</pre><br />
<br />
<pre>pg_ctl stop [-W] [-t seconds] [-s] [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ]</pre><br />
<br />
<pre>pg_ctl restart [-w] [-t seconds] [-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o options]</pre><br />
<br />
<pre>pg_ctl reload [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl status [-D datadir]</pre><br />
<br />
<pre>pg_ctl promote [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl kill signal_name process_id</pre><br />
<br />
<pre>pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]</pre><br />
<br />
<pre>pg_ctl unregister [-N servicename]</pre><br />
<br />
'''Description'''<br />
<br />
pg_ctl 是一个用于初始化,启动,停止, 或者重起 PostgreSQL 后端服务器(postgres), 或者显示一个运行着的服务器的状态的工具, 尽管我们可以手动启动服务器,但是 pg_ctl 封装了重新定向日志输出,与终端和进程组合理分离,以及另外提供了方便的选项用于有控制的关闭。<br />
<br />
在Init或initdb模式下创建一个新的PostgreSQL数据库集群。一个数据库集群是由一个服务进程管理的多个数据库集合。该模式调用initdb命令。详情请参考initdb。<br />
<br />
在 start 模式里会启动一个新的服务器。服务器是在后台启动的,标准输入被附着到了 /dev/null(在Windows下是nul) 上。在类UNIX系统中,缺省情况下服务器的标准输出和标准错误输出都被发送到pg_ctl's的标准输出(不是标准错误)。pg_ctl的标准输出被重定向到一个文件或者通过管理输送给另一个进行,比如日志处理程序rotatelogs。否则postgres将把它的输出写到控制终端(从后台)并且不会脱离shell进程组。在Windows中,缺省情况下服务器的标准输出和标准错误被发送到终端。这些缺省行为可以通过-l选项来指定log重定向到某个文件。推荐使用-l或者输出重定向。<br />
<br />
在 stop 模式下,那个正在特定数据目录运行的服务器被关闭。你可以用 -m 选项选择三种不同的关闭模式:"Smart" 模式(也是缺省模式)等待所有客户端中断联接且正在进行的备份将被终止。如果服务器不是hot standby,恢复和流复制也将被在客户端连接中断后退出。"Fast" 模式并不等待客户端中断联接。 所有活跃事务都被回滚并且客户端都强制断开。 "Immediate" 模式将在没有干净关闭的情况下退出。这么做将导致在重新启动的时候的恢复。<br />
<br />
restart 实际上是先执行一个停止,然后紧跟一个启动。它允许变换postgres命令行的选项。<br />
<br />
reload 模式简单地给postmaster发送一个 SIGHUP 信号,导致它重新读取她的配置文件 (postgresql.conf,pg_hba.conf 等等) 这样就允许修改配置文件选项而不用完全重启系统来使之生效。<br />
<br />
status 模式检查一个服务器是否在指定的数据目录运行, 如果是,那么显示其 PID 和调用它的命令行选项。<br />
<br />
生产模式,运行在某个数据目录的standby服务器将退出恢复并进开始进行读写操作。<br />
<br />
kill 模式允许你给一个指定的进程发送信号。这个功能对 Microsoft Windows 特别有用,因为那里没有 kill 命令。 使用 --help 查看支持的信号的名字的列表。<br />
<br />
register 模式允许你在 Microsoft Windows 上注册一个系统服务。-S选项用于设定启运类型,或者“自动”(系统启动时自动启动)或者“后台”(后台运行)。<br />
<br />
unregister 模式允许你在 Microsoft Windows 上删除这个系统服务, 这个系统服务是前面用 register 命令注册的。<br />
<br />
'''Options'''<br />
<br />
-c<br />
:允许服务器崩溃时产生core文件,只要其所在的平台允许,所有的信息将会放进core文件中。异常进程产生的堆栈信息对于调试或问题诊断非常有用。<br />
<br />
-D datadir<br />
:声明该数据库文件的文件系统位置。 如果忽略这个选项,使用环境变量 PGDATA。<br />
<br />
-l filename<br />
:把服务器日志输出附加在 filename 文件上。 如果该文件不存在,那么创建它。umask设置为 077, 因此缺省时是不允许从其它用户向日志文件访问的。<br />
<br />
-m mode<br />
:声明关闭模式。mode 可以是smart, fast, 或者 immediate,或者是这三个之一的第一个字母。若不指定,缺省是smart模式。<br />
<br />
-o options<br />
:声明要直接传递给 postgres 的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-o initdb-options<br />
:指定要直接传递给initdb命令行的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-p path<br />
:声明 postgres 可执行文件的位置。缺省时postgres是从和pg_ctl相同的目录取出,如果不是, 那么就是写死的安装目录。除非你想干点什么特别的事情,并且得到类似没有找到postgres这样的错误,否则没有必要使用这个选项。<br />
<br />
:在init模式,该选项相同的用来指定initdb可执行文件的位置。<br />
<br />
-s<br />
:只打印错误,而不打印提示性信息。<br />
<br />
-t<br />
:等待启动或关闭完成的最大秒数。缺省是60秒。<br />
<br />
-w<br />
:等待启动或者关闭的完成。 这个参数是关闭时的缺省值,但不是启动时的缺省值。等待启动时,pg_ctl不断地尝试连接服务器。等待关闭时,pg_ctl等待服务器删除其PID文件。pg_ctl基于启动或关闭成功与否返回退出代码。<br />
<br />
-W<br />
:不等待启动或者停止的完成。这是启动和重起的缺省。<br />
<br />
'''Options for Windows'''<br />
<br />
-N servicename<br />
:要注册的系统服务的名字。这个名字将用于服务名和显示名。<br />
<br />
-P password<br />
:用户启动服务的口令。<br />
<br />
-S start-type<br />
:注册系统服务的启动类型。启动类型可以是auto或demand,或者二者的首字母。如果不指定,缺省值是auto.<br />
<br />
-U username<br />
:启动服务的用户名。对于domain用户,格式为DOMAIN\username.<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
pg_ctl 像其他PostgreSQL实用工具一样,同样使用libpq支持的环境变量(参照Section 31.13)。其他的变量,参照postgres.<br />
<br />
'''Files'''<br />
<br />
postmaster.pid<br />
:该文件是否存在用于帮助pg_ctl判断服务器是否正在运行。<br />
<br />
postmaster.opts<br />
:如果该文件存在于数据目录,pg_ctl(在启动模式下)将以该文件的内容为参数传递给postgres,除非被-o选项覆盖。该文件内容也会在状态模式下显示。<br />
<br />
'''Examples'''<br />
<br />
'''Starting the Server'''<br />
<br />
启动服务器:<br />
<pre>$ pg_ctl start</pre><br />
<br />
启动服务器,等待至服务器接受连接:<br />
<pre>$ pg_ctl -w start</pre><br />
<br />
使用端口5433启动服务器,同时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" start</pre><br />
<br />
'''Stopping the Server'''<br />
<br />
关闭服务器:<br />
<pre>$ pg_ctl stop</pre><br />
<br />
选项-m用来控制服务器关闭方式:<br />
<pre>$ pg_ctl stop -m fast</pre><br />
<br />
'''Restarting the Server'''<br />
<br />
服务器重启几乎等价于关闭后再启动,只是pg_ctl保存并重用了传递给之前进程的命令行选项。最简单的重启:<br />
<pre>$ pg_ctl restart</pre><br />
<br />
重启服务器并等待其关闭并重启:<br />
<pre>$ pg_ctl -w restart</pre><br />
<br />
使用端口5433重启,重启时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" restart</pre><br />
<br />
'''Showing the Server Status'''<br />
<br />
下面是简单的pg_ctl状态输出例子:<br />
<pre><br />
$ pg_ctl status<br />
pg_ctl: server is running (PID: 13718)<br />
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"<br />
</pre><br />
<br />
This is the command line that would be invoked in restart mode.<br />
<br />
'''See Also'''<br />
<br />
initdb, postgres<br />
<br />
== pg_resetxlog ==<br />
<br />
'''Name'''<br />
<br />
pg_resetxlog -- reset the write-ahead log and other control information of a PostgreSQL database cluster<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_resetxlog [-f] [-n] [-ooid ] [-x xid ] [-e xid_epoch ] [-m mxid ] [-O mxoff ] [-l timelineid,fileid,seg ] datadir</pre><br />
<br />
'''Description'''<br />
<br />
pg_resetxlog clears the write-ahead log (WAL) and optionally resets some other control information stored in the pg_control file. This function is sometimes needed if these files have become corrupted. It should be used only as a last resort, when the server will not start due to such corruption.<br />
<br />
After running this command, it should be possible to start the server, but bear in mind that the database might contain inconsistent data due to partially-committed transactions. You should immediately dump your data, run initdb, and reload. After reload, check for inconsistencies and repair as needed.<br />
<br />
This utility can only be run by the user who installed the server, because it requires read/write access to the data directory. For safety reasons, you must specify the data directory on the command line. pg_resetxlog does not use the environment variable PGDATA.<br />
<br />
If pg_resetxlog complains that it cannot determine valid data for pg_control, you can force it to proceed anyway by specifying the -f (force) switch. In this case plausible values will be substituted for the missing data. Most of the fields can be expected to match, but manual assistance might be needed for the next OID, next transaction ID and epoch, next multitransaction ID and offset, and WAL starting address fields. These fields can be set using the switches discussed below. If you are not able to determine correct values for all these fields, -f can still be used, but the recovered database must be treated with even more suspicion than usual: an immediate dump and reload is imperative. Do not execute any data-modifying operations in the database before you dump, as any such action is likely to make the corruption worse.<br />
<br />
The -o, -x, -e, -m, -O, and -l switches allow the next OID, next transaction ID, next transaction ID's epoch, next multitransaction ID, next multitransaction offset, and WAL starting address values to be set manually. These are only needed when pg_resetxlog is unable to determine appropriate values by reading pg_control. Safe values can be determined as follows:<br />
<br />
:A safe value for the next transaction ID (-x) can be determined by looking for the numerically largest file name in the directory pg_clog under the data directory, adding one, and then multiplying by 1048576. Note that the file names are in hexadecimal. It is usually easiest to specify the switch value in hexadecimal too. For example, if 0011 is the largest entry in pg_clog, -x 0x1200000 will work (five trailing zeroes provide the proper multiplier).<br />
<br />
:A safe value for the next multitransaction ID (-m) can be determined by looking for the numerically largest file name in the directory pg_multixact/offsets under the data directory, adding one, and then multiplying by 65536. As above, the file names are in hexadecimal, so the easiest way to do this is to specify the switch value in hexadecimal and add four zeroes.<br />
<br />
:A safe value for the next multitransaction offset (-O) can be determined by looking for the numerically largest file name in the directory pg_multixact/members under the data directory, adding one, and then multiplying by 65536. As above, the file names are in hexadecimal, so the easiest way to do this is to specify the switch value in hexadecimal and add four zeroes.<br />
<br />
:The WAL starting address (-l) should be larger than any WAL segment file name currently existing in the directory pg_xlog under the data directory. These names are also in hexadecimal and have three parts. The first part is the "timeline ID" and should usually be kept the same. Do not choose a value larger than 255 (0xFF) for the third part; instead increment the second part and reset the third part to 0. For example, if 00000001000000320000004A is the largest entry in pg_xlog, -l 0x1,0x32,0x4B will work; but if the largest entry is 000000010000003A000000FF, choose -l 0x1,0x3B,0x0 or more.<br />
<br />
<pre><br />
Note: pg_resetxlog itself looks at the files in pg_xlog and chooses a default -l setting beyond the last existing file name. Therefore, manual adjustment of -l should only be needed if you are aware of WAL segment files that are not currently present in pg_xlog, such as entries in an offline archive; or if the contents of pg_xlog have been lost entirely.<br />
</pre><br />
<br />
:There is no comparably easy way to determine a next OID that's beyond the largest one in the database, but fortunately it is not critical to get the next-OID setting right.<br />
<br />
:The transaction ID epoch is not actually stored anywhere in the database except in the field that is set by pg_resetxlog, so any value will work so far as the database itself is concerned. You might need to adjust this value to ensure that replication systems such as Slony-I work correctly — if so, an appropriate value should be obtainable from the state of the downstream replicated database.<br />
<br />
The -n (no operation) switch instructs pg_resetxlog to print the values reconstructed from pg_control and then exit without modifying anything. This is mainly a debugging tool, but can be useful as a sanity check before allowing pg_resetxlog to proceed for real.<br />
<br />
The -V and --version options print the pg_resetxlog version and exit. The options -? and --help show supported arguments, and exit.<br />
<br />
'''Notes'''<br />
<br />
This command must not be used when the server is running. pg_resetxlog will refuse to start up if it finds a server lock file in the data directory. If the server crashed then a lock file might have been left behind; in that case you can remove the lock file to allow pg_resetxlog to run. But before you do so, make doubly certain that there is no server process still alive.<br />
<br />
== postgres ==<br />
-- PostgreSQL database server<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208159.1参考32013-09-20T07:00:59Z<p>Horen: /* pg_ctl */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
<br />
'''Name'''<br />
<br />
pg_ctl -- 初始化、启动、停止和重起 PostgreSQL<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_ctl init[db] [-s] [-D datadir] [-o initdb-options]</pre><br />
<br />
<pre>pg_ctl start [-w] [-t seconds] [-s] [-D datadir] [-l filename] [-o options] [-p path] [-c]</pre><br />
<br />
<pre>pg_ctl stop [-W] [-t seconds] [-s] [-D datadir] [-m s[mart] | f[ast] | i[mmediate] ]</pre><br />
<br />
<pre>pg_ctl restart [-w] [-t seconds] [-s] [-D datadir] [-c] [-m s[mart] | f[ast] | i[mmediate] ] [-o options]</pre><br />
<br />
<pre>pg_ctl reload [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl status [-D datadir]</pre><br />
<br />
<pre>pg_ctl promote [-s] [-D datadir]</pre><br />
<br />
<pre>pg_ctl kill signal_name process_id</pre><br />
<br />
<pre>pg_ctl register [-N servicename] [-U username] [-P password] [-D datadir] [-S a[uto] | d[emand] ] [-w] [-t seconds] [-s] [-o options]</pre><br />
<br />
<pre>pg_ctl unregister [-N servicename]</pre><br />
<br />
'''Description'''<br />
<br />
pg_ctl 是一个用于初始化,启动,停止, 或者重起 PostgreSQL 后端服务器(postgres), 或者显示一个运行着的服务器的状态的工具, 尽管我们可以手动启动服务器,但是 pg_ctl 封装了重新定向日志输出,与终端和进程组合理分离,以及另外提供了方便的选项用于有控制的关闭。<br />
<br />
在Init或initdb模式下创建一个新的PostgreSQL数据库集群。一个数据库集群是由一个服务进程管理的多个数据库集合。该模式调用initdb命令。详情请参考initdb。<br />
<br />
在 start 模式里会启动一个新的服务器。服务器是在后台启动的,标准输入被附着到了 /dev/null(在Windows下是nul) 上。在类UNIX系统中,缺省情况下服务器的标准输出和标准错误输出都被发送到pg_ctl's的标准输出(不是标准错误)。pg_ctl的标准输出被重定向到一个文件或者通过管理输送给另一个进行,比如日志处理程序rotatelogs。否则postgres将把它的输出写到控制终端(从后台)并且不会脱离shell进程组。在Windows中,缺省情况下服务器的标准输出和标准错误被发送到终端。这些缺省行为可以通过-l选项来指定log重定向到某个文件。推荐使用-l或者输出重定向。<br />
<br />
在 stop 模式下,那个正在特定数据目录运行的服务器被关闭。你可以用 -m 选项选择三种不同的关闭模式:"Smart" 模式(也是缺省模式)等待所有客户端中断联接且正在进行的备份将被终止。如果服务器不是hot standby,恢复和流复制也将被在客户端连接中断后退出。"Fast" 模式并不等待客户端中断联接。 所有活跃事务都被回滚并且客户端都强制断开。 "Immediate" 模式将在没有干净关闭的情况下退出。这么做将导致在重新启动的时候的恢复。<br />
<br />
restart 实际上是先执行一个停止,然后紧跟一个启动。它允许变换postgres命令行的选项。<br />
<br />
reload 模式简单地给postmaster发送一个 SIGHUP 信号,导致它重新读取她的配置文件 (postgresql.conf,pg_hba.conf 等等) 这样就允许修改配置文件选项而不用完全重启系统来使之生效。<br />
<br />
status 模式检查一个服务器是否在指定的数据目录运行, 如果是,那么显示其 PID 和调用它的命令行选项。<br />
<br />
生产模式,运行在某个数据目录的standby服务器将退出恢复并进开始进行读写操作。<br />
<br />
kill 模式允许你给一个指定的进程发送信号。这个功能对 Microsoft Windows 特别有用,因为那里没有 kill 命令。 使用 --help 查看支持的信号的名字的列表。<br />
<br />
register 模式允许你在 Microsoft Windows 上注册一个系统服务。-S选项用于设定启运类型,或者“自动”(系统启动时自动启动)或者“后台”(后台运行)。<br />
<br />
unregister 模式允许你在 Microsoft Windows 上删除这个系统服务, 这个系统服务是前面用 register 命令注册的。<br />
<br />
'''Options'''<br />
<br />
-c<br />
:允许服务器崩溃时产生core文件,只要其所在的平台允许,所有的信息将会放进core文件中。异常进程产生的堆栈信息对于调试或问题诊断非常有用。<br />
<br />
-D datadir<br />
:声明该数据库文件的文件系统位置。 如果忽略这个选项,使用环境变量 PGDATA。<br />
<br />
-l filename<br />
:把服务器日志输出附加在 filename 文件上。 如果该文件不存在,那么创建它。umask设置为 077, 因此缺省时是不允许从其它用户向日志文件访问的。<br />
<br />
-m mode<br />
:声明关闭模式。mode 可以是smart, fast, 或者 immediate,或者是这三个之一的第一个字母。若不指定,缺省是smart模式。<br />
<br />
-o options<br />
:声明要直接传递给 postgres 的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-o initdb-options<br />
:指定要直接传递给initdb命令行的选项。<br />
<br />
:参数通常都用单或者双引号包围以保证它们作为一个整体传递。<br />
<br />
-p path<br />
:声明 postgres 可执行文件的位置。缺省时postgres是从和pg_ctl相同的目录取出,如果不是, 那么就是写死的安装目录。除非你想干点什么特别的事情,并且得到类似没有找到postgres这样的错误,否则没有必要使用这个选项。<br />
<br />
:在init模式,该选项相同的用来指定initdb可执行文件的位置。<br />
<br />
-s<br />
:只打印错误,而不打印提示性信息。<br />
<br />
-t<br />
:等待启动或关闭完成的最大秒数。缺省是60秒。<br />
<br />
-w<br />
:等待启动或者关闭的完成。 这个参数是关闭时的缺省值,但不是启动时的缺省值。等待启动时,pg_ctl不断地尝试连接服务器。等待关闭时,pg_ctl等待服务器删除其PID文件。pg_ctl基于启动或关闭成功与否返回退出代码。<br />
<br />
-W<br />
:不等待启动或者停止的完成。这是启动和重起的缺省。<br />
<br />
'''Options for Windows'''<br />
<br />
-N servicename<br />
:要注册的系统服务的名字。这个名字将用于服务名和显示名。<br />
<br />
-P password<br />
:用户启动服务的口令。<br />
<br />
-S start-type<br />
:注册系统服务的启动类型。启动类型可以是auto或demand,或者二者的首字母。如果不指定,缺省值是auto.<br />
<br />
-U username<br />
:启动服务的用户名。对于domain用户,格式为DOMAIN\username.<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录位置<br />
<br />
pg_ctl 像其他PostgreSQL实用工具一样,同样使用libpq支持的环境变量(参照Section 31.13)。其他的变量,参照postgres.<br />
<br />
'''Files'''<br />
<br />
postmaster.pid<br />
:该文件是否存在用于帮助pg_ctl判断服务器是否正在运行。<br />
<br />
postmaster.opts<br />
:如果该文件存在于数据目录,pg_ctl(在启动模式下)将以该文件的内容为参数传递给postgres,除非被-o选项覆盖。该文件内容也会在状态模式下显示。<br />
<br />
'''Examples'''<br />
<br />
'''Starting the Server'''<br />
<br />
启动服务器:<br />
<pre>$ pg_ctl start</pre><br />
<br />
启动服务器,等待至服务器接受连接:<br />
<pre>$ pg_ctl -w start</pre><br />
<br />
使用端口5433启动服务器,同时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" start</pre><br />
<br />
'''Stopping the Server'''<br />
<br />
关闭服务器:<br />
<pre>$ pg_ctl stop</pre><br />
<br />
选项-m用来控制服务器关闭方式:<br />
<pre>$ pg_ctl stop -m fast</pre><br />
<br />
'''Restarting the Server'''<br />
<br />
服务器重启几乎等价于关闭后再启动,只是pg_ctl保存并重用了传递给之前进程的命令行选项。最简单的重启:<br />
<pre>$ pg_ctl restart</pre><br />
<br />
重启服务器并等待其关闭并重启:<br />
<pre>$ pg_ctl -w restart</pre><br />
<br />
使用端口5433重启,重启时禁用fsync:<br />
<pre>$ pg_ctl -o "-F -p 5433" restart</pre><br />
<br />
'''Showing the Server Status'''<br />
<br />
下面是简单的pg_ctl状态输出例子:<br />
<pre><br />
$ pg_ctl status<br />
pg_ctl: server is running (PID: 13718)<br />
/usr/local/pgsql/bin/postgres "-D" "/usr/local/pgsql/data" "-p" "5433" "-B" "128"<br />
</pre><br />
<br />
This is the command line that would be invoked in restart mode.<br />
<br />
'''See Also'''<br />
<br />
initdb, postgres<br />
<br />
== pg_resetxlog ==<br />
-- reset the write-ahead log and other control information of a PostgreSQL database cluster<br />
== postgres ==<br />
-- PostgreSQL database server<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208149.1参考32013-09-20T06:34:26Z<p>Horen: /* pg_controldata */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
'''Name'''<br />
<br />
pg_controldata -- 显示PostgreSQL数据库集群的控制信息<br />
<br />
'''Synopsis'''<br />
<br />
<pre>pg_controldata [option] [datadir]</pre><br />
<br />
'''Description'''<br />
<br />
pg_controldata 打印initdb初始化过的信息,比如系统表的版本。它也可以显示预写日志和检查点处理的信息。这个信息是集群层面的,不针对任何特定的数据库。<br />
<br />
该工具仅可以被初始化该集群的用户执行,因为它需要对数据库数据目录的访问权限。你可以通过命令行指定数据目录也可以通过设置环境变量 PGDATA。该工具还支持-V和--version选项,该选项打印pg_controldata版本并退出。它还支持选项-?和--help,输出命令行帮助信息。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:缺省数据目录<br />
<br />
== pg_ctl ==<br />
-- initialize, start, stop, or control a PostgreSQL server<br />
== pg_resetxlog ==<br />
-- reset the write-ahead log and other control information of a PostgreSQL database cluster<br />
== postgres ==<br />
-- PostgreSQL database server<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208139.1参考32013-09-20T05:31:35Z<p>Horen: /* initdb */</p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
<br />
'''Name'''<br />
<br />
initdb -- 创建一个新的 PostgreSQL数据库集群<br />
<br />
'''Synopsis'''<br />
<br />
<pre>initdb [option...] --pgdata | -D directory</pre><br />
<br />
'''Description'''<br />
<br />
initdb 创建一个新的 PostgreSQL 数据库集群。 一个数据库集群是由单个服务器实例管理的数据库集合。<br />
<br />
创建数据库集群包括创建数据库数据的宿主目录,生成共享的系统表(不属于任何特定数据库的表)和创建 template1 和 postgres 数据库。当你以后再创建一个新数据库时, template1 数据库里所有内容都会拷贝过来。(因此,任何在 template1 里面安装的东西都自动拷贝到之后创建的数据库中。) postgres 数据库是一个缺省数据库,用于给用户、工具或者第三方应用提供缺省数据库。<br />
<br />
尽管initdb会尝试创建相应的数据目录, 但经常会发生它没有权限做这些事情的情况。因为所需要的目录的父目录通常是 root 所有的目录。 要初始化这种设置,用 root 创建一个空数据目录, 然后用 chown 把该目录的所有权交给数据库用户帐号, 然后 su 成数据库用户,最后以数据库用户身份运行 initdb。<br />
<br />
initdb 必须以服务器进程所有者身份运行,因为服务器需要initdb所创建文件和目录的访问权限。因为服务器不能以root身份运行,你也不能以root身份运行initdb。(事实上会被拒绝。)<br />
<br />
initdb 初始化该数据库集群的缺省区域和字符集编码。字符编码排序(LC_COLLATE)和字符集表(LC_CTYPE, 也就是,大写,小写,数字等)可在数据库创建时单独设置。initdb 决定 template1 数据库的编码,而该编码将成为所有其它数据库的缺省。<br />
<br />
要修改缺省编码排序或者字符集表,使用--lc-collate和--lc-ctype选项。使用 C 或 POSIX 之外的字符编码排序还会有性能影响。因此在运行initdb时选取正确的设置非常重要。<br />
<br />
其余的设置可以在服务器启动后改变。也可以使用--locale设置缺省值,包括编码排序和字符集表。服务器的设置可通过SHOW ALL显示。更多细节可以参考Section 22.1。<br />
<br />
若要改变缺省编码,使用--encoding。更多细节可以参考Section 22.3。<br />
<br />
'''Options'''<br />
<br />
-A authmethod<br />
--auth=authmethod<br />
:这个选项声明本地用户在 pg_hba.conf 里面使用的认证方法。 除非你相信所有你的系统上的本地用户,否则不要使用 trust。 Trust 是所有安装的缺省。<br />
<br />
-D directory<br />
--pgdata=directory<br />
:这个选项声明数据库集群应该存放在哪个目录。 这是initdb需要的唯一信息,但是你可以通过设置 PGDATA 环境变量来避免键入, 这样做可能方便一些,因为稍后数据库服务器(postmaster)可以通过同一个变量找到数据库目录。<br />
<br />
-E encoding<br />
--encoding=encoding<br />
:选择模板数据库的编码方式。这将是你以后创建的数据库的缺省编码方式, 除非你创建数据库时覆盖了它。缺省是从区域设置中获得的,如果没有区域设置,就是 SQL_ASCII。 PostgreSQL 服务器支持的字符集在 Section 22.3.1 里描述。<br />
<br />
--locale=locale<br />
:为数据库集群设置缺省的区域。如果没有声明这个选项,那么区域 是从 initdb 运行的环境中继承过来的。 区域设置在 Section 22.1 里描述。<br />
<br />
--lc-collate=locale<br />
--lc-ctype=locale<br />
--lc-messages=locale<br />
--lc-monetary=locale<br />
--lc-numeric=locale<br />
--lc-time=locale<br />
:类似 --locale,但是只设置特殊范畴的区域。<br />
<br />
--no-locale<br />
:等价于--locale=C。<br />
<br />
--pwfile=filename<br />
:使initdb从文件中读取超级用户的密码。密码位于该文件中第一行。<br />
<br />
--text-search-config=CFG<br />
:设置缺省文本搜索。参看default_text_search_config获得更多信息。<br />
<br />
-U username<br />
--username=username<br />
:选择数据库超级用户的用户名。缺省是运行 initdb 的用户的有效用户。 超级用户的名字是什么并不重要, 但是我们可以选择习惯的名字 postgres,即使操作系统的用户名字不一样也无所谓。<br />
<br />
-W<br />
--pwprompt<br />
:令 initdb 提示输入数据库超级用户的口令。 如果你不准备使用口令认证,这个东西并不重要。 否则你将不能使用口令认证直到你设置了口令。<br />
<br />
-X directory<br />
--xlogdir=directory<br />
:该选项指定事务日志存放的路径。<br />
<br />
其他的,不常用的参数同样可用:<br />
<br />
-d<br />
--debug<br />
:从初始化后端打印调试输出以及一些其它的一些普通用户不太感兴趣的信息。 初始化后端是 initdb 用于创建系统表的程序。 这个选项生成大量非常烦人的输出。<br />
<br />
-L directory<br />
:告诉 initdb 到哪里找初始化数据库所需要的输入文件。 通常是不必要的。如果需要你明确声明的话,程序会提示你输入。<br />
<br />
-n<br />
--noclean<br />
:缺省时,当initdb 发现一些错误妨碍它完成创建数据库集群的工作时, 它将在检测到不能结束工作之前将其创建的所有文件删除。 这个选项禁止任何清理动作,因而对调试很有用。<br />
<br />
-V<br />
--version<br />
:打印initdb版本后退出。<br />
<br />
-?<br />
--help<br />
:显示initdb命令行参数的帮助信息后退出。<br />
<br />
'''Environment'''<br />
<br />
PGDATA<br />
:声明数据库集群存储的目录;可以用 -D 选项覆盖。<br />
<br />
这个工具,和其他PostgreSQL工具一样都使用libpq支持的环境变量(参照Section31.13)。<br />
<br />
'''Notes'''<br />
<br />
initdb 也可以被pg_ctl initdb调用。<br />
<br />
'''See Also'''<br />
<br />
pg_ctl, postgres<br />
<br />
== pg_controldata ==<br />
-- display control information of a PostgreSQL database cluster<br />
== pg_ctl ==<br />
-- initialize, start, stop, or control a PostgreSQL server<br />
== pg_resetxlog ==<br />
-- reset the write-ahead log and other control information of a PostgreSQL database cluster<br />
== postgres ==<br />
-- PostgreSQL database server<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208129.1参考32013-09-20T05:13:00Z<p>Horen: </p>
<hr />
<div>''' 参考3 服务器应用 '''<br />
<br />
这部分包括 PostgreSQL 服务器应用和支持工具的参考信息。这些命令只能用于在数据库服务器所在的主机上运行。其它工具程序在 Reference II,客户端应用 中列出。<br />
<br />
== initdb == <br />
-- create a new PostgreSQL database cluster<br />
== pg_controldata ==<br />
-- display control information of a PostgreSQL database cluster<br />
== pg_ctl ==<br />
-- initialize, start, stop, or control a PostgreSQL server<br />
== pg_resetxlog ==<br />
-- reset the write-ahead log and other control information of a PostgreSQL database cluster<br />
== postgres ==<br />
-- PostgreSQL database server<br />
== postmaster ==<br />
-- PostgreSQL database server</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E5%8F%82%E8%80%833&diff=208119.1参考32013-09-20T05:06:22Z<p>Horen: Created page with "工"</p>
<hr />
<div>工</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E6%96%87%E6%A1%A3%E7%BF%BB%E8%AF%91%E9%A1%B9%E7%9B%AE&diff=208109.1文档翻译项目2013-09-20T05:04:46Z<p>Horen: </p>
<hr />
<div>参考文档<br />
*[http://www.pgsqldb.org/mwiki/index.php/PostgreSQL_8.3_%E6%96%87%E6%A1%A3 8.3 wiki版本]<br />
*[http://www.pgsqldb.org/pgsqldoc-8.1c/index.html 8.1中文文档]<br />
*[http://wiki.postgresql.org/images/7/7b/PostgreSQL8.2.3doc.zip 8.2.3中文文档]<br />
*[http://developer.postgresql.org/pgdocs/postgres/index.html 9.1文档]<br />
*[http://wiki.postgresql.org/wiki/Help:Formatting wiki语法参考]<br />
<br />
校对的时候,使用<del> </del>标记原来的内容,然后写上修改后的东西。<br />
{|border="1"<br />
|其他语言模块<br />
|模块名字<br />
|标注者<br />
|-<br />
|python<br />
|[[psycopy]]<br />
|冇得弹<br />
|-<br />
|.net<br />
|[[npgsql]]<br />
|冇得弹<br />
|-<br />
|php<br />
|直接支持<br />
|冇得弹<br />
|}<br />
翻译任务<br />
{| border=1<br />
| 部分 <br />
| 章节<br />
| 标题<br />
| 翻译志愿者<br />
| 状态<br />
| 开始时间<br />
| 计划完成时间<br />
| 备注<br />
| 文件<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 1<br />
| Getting Started<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
| [[9.1第一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 2<br />
| The SQL Language<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第二章]]<br />
|-<br />
| Tutorial<br />
| 3<br />
| Advanced Features<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第三章]]<br />
|-<br />
| The SQL Language<br />
| 4<br />
| SQL Syntax<br />
| 冇得弹<br />
| 89%<br />
|<br />
|<br />
|<br />
|[[9.1第四章]]<br />
|-<br />
| The SQL Language<br />
| 5<br />
| Data Definition<br />
| 冇得弹<br />
| 98%<br />
|<br />
|<br />
|<br />
|[[9.1第五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| The SQL Language<br />
| 6<br />
| Data Manipulation<br />
| 冇得弹<br />
| '''完美'''<br />
| <br />
| <br />
| 这章的内容和8.1文档一样<br />
|[[9.1第六章]]<br />
|-<br />
| The SQL Language<br />
| 7<br />
| Queries<br />
| 冇得弹<br />
| <br />
| <br />
| <br />
| <br />
|[[9.1第七章]]<br />
|-<br />
| The SQL Language<br />
| 8<br />
| Data Types<br />
| 冇得弹<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第八章]]<br />
|-<br />
| The SQL Language<br />
| 9<br />
| Functions and Operators<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第九章]]<br />
|-<br />
| The SQL Language<br />
| 10<br />
| Type Conversion<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十章]]<br />
|-<br />
| The SQL Language<br />
| 11<br />
| Indexes<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十一章]]<br />
|-<br />
| The SQL Language<br />
| 12<br />
| Full Text Search<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十二章]]<br />
|-<br />
| The SQL Language<br />
| 13<br />
| Concurrency Control<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十三章]]<br />
|-<br />
| The SQL Language<br />
| 14<br />
| Performance Tips<br />
| 冇得弹<br />
|开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十四章]]<br />
|-<br />
| Server Administration<br />
| 15<br />
| Installation from Source Code<br />
| 紫轩<br />
| 基本完成<br />
|<br />
|<br />
|<br />
|[[9.1第十五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 16<br />
| Installation from Source Code on Windows<br />
| 紫轩<br />
| 完成 校正中 (iihero补校)<br />
|<br />
|<br />
| 有些计算机专业用语不懂,恐怕会让人误解<br />
|[[9.1第十六章]]<br />
|-<br />
| Server Administration<br />
| 17<br />
| Server Setup and Operation<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十七章]]<br />
|-<br />
| Server Administration<br />
| 18<br />
| Server Configuration<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十八章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 19<br />
| Client Authentication<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第十九章]]<br />
|-<br />
| Server Administration<br />
| 20<br />
| Database Roles<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十章]]<br />
|-<br />
| Server Administration<br />
| 21<br />
| Managing Databases<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 22<br />
| Localization<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十二章]]<br />
|-<br />
| Server Administration<br />
| 23<br />
| Routine Database Maintenance Tasks<br />
| 小燕子 <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十三章]]<br />
|-<br />
| Server Administration<br />
| 24<br />
| Backup and Restore<br />
| 星海游侠<br />
| 完成了一半<br />
|<br />
|<br />
|<br />
|[[9.1第二十四章]]<br />
|-<br />
| Server Administration<br />
| 25<br />
| High Availability, Load Balancing, and Replication<br />
| 星海游侠<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十五章]]<br />
|-<br />
| Server Administration<br />
| 26<br />
| Recovery Configuration<br />
| 星海游侠<br />
| 完成,待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十六章]]<br />
|-<br />
| Server Administration<br />
| 27<br />
| Monitoring Database Activity<br />
| 泥鳅<br />
| 完成<br />
| <br />
|<br />
| 本章的中文部分是基于9.0的,9.1新增的内容没有翻译还是英文<br />
|[[9.1第二十七章]]<br />
|-<br />
| Server Administration<br />
| 28<br />
| Monitoring Disk Usage<br />
| 泥鳅<br />
| 完成,很简单,不需要校验<br />
|<br />
|<br />
|<br />
|[[9.1第二十八章]]<br />
|-<br />
| Server Administration<br />
| 29<br />
| Reliability and the Write-Ahead Log<br />
| 小燕子<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十九章]]<br />
|-<br />
| Server Administration<br />
| 30<br />
| Regression Tests<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十章]]<br />
|-style="font-style:italic;color:green;"<br />
| Client Interfaces<br />
| 31<br />
| ibpq - C Library<br />
| 泥鳅<br />
| Working in progress<br />
| 2011-7-17<br />
| 工作量极大,不好估算时间<br />
|<br />
|[[9.1第三十一章]]<br />
|-<br />
| Client Interfaces<br />
| 32<br />
| Large Objects<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十二章]]<br />
|-<br />
| Client Interfaces<br />
| 33<br />
| ECPG - Embedded SQL in C<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十三章]]<br />
|-<br />
| Client Interfaces<br />
| 34<br />
| The Information Schema<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十四章]]<br />
|-style="font-style:italic;color:red;"<br />
| Server Programming<br />
| 35<br />
| Extending SQL<br />
| iihero<br />
| '''Start working'''<br />
|<br />
|<br />
|<br />
|[[9.1第三十五章]]<br />
|-<br />
| Server Programming<br />
| 36<br />
| Triggers<br />
| 任洪彩<br />
| 待校<br />
| <br />
|<br />
|<br />
|[[9.1第三十六章]]<br />
|-<br />
| Server Programming<br />
| 37<br />
| The Rule System<br />
| 小燕子<br />
| i'm here<br />
|<br />
|<br />
|<br />
|[[9.1第三十七章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Programming<br />
| 38<br />
| Procedural Languages<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第三十八章]]<br />
|-<br />
| Server Programming<br />
| 39<br />
| PL/pgSQL - SQL Procedural Language<br />
| 杨拼<br />
|开始了。。。<br />
|<br />
|<br />
|<br />
|[[9.1第三十九章]]<br />
|-<br />
| Server Programming<br />
| 40<br />
| PL/Tcl - Tcl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十章]]<br />
|-<br />
| Server Programming<br />
| 41<br />
| PL/Perl - Perl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十一章]]<br />
|-<br />
| Server Programming<br />
| 42<br />
| PL/Python - Python Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十二章]]<br />
|-<br />
| Server Programming<br />
| 43<br />
| Server Programming Interface<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十三章]]<br />
|-<br />
| Reference<br />
| I<br />
| SQL Commands<br />
| gvim<br />
| 开始 23/150<br />
|<br />
|<br />
|<br />
|[[9.1参考1]]<br />
|-<br />
| Reference<br />
| II<br />
| PostgreSQL Client Applications<br />
| eastday<br />
| 刚刚开始。<br />
|<br />
|<br />
|大多内容和8.1相同.<br />
|[[9.1参考2]]<br />
|-<br />
| Reference<br />
| III<br />
| PostgreSQL Server Applications<br />
| 任洪彩<br />
| 马上开始<br />
|<br />
|<br />
|<br />
|[[9.1参考3]]<br />
|- style="font-style:italic;color:red;"<br />
| Internals<br />
| 44<br />
| Overview of PostgreSQL Internals<br />
| Galy<br />
| 着手<br />
|<br />
|<br />
|<br />
| [[9.1第44章]]<br />
|-<br />
| Internals<br />
| 45<br />
| System Catalogs<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第45章]]<br />
|-<br />
| Internals<br />
| 46<br />
| Frontend/Backend Protocol<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第46章]]<br />
|-<br />
| Internals<br />
| 47<br />
| PostgreSQL Coding Conventions<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第47章]]<br />
|-<br />
| Internals<br />
| 48<br />
| Native Language Support<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第48章]]<br />
|-<br />
| Internals<br />
| 49<br />
| Writing A Procedural Language Handler<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第49章]]<br />
|-<br />
| Internals<br />
| 50<br />
| Genetic Query Optimizer<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第50章]]<br />
|-<br />
| Internals<br />
| 51<br />
| Index Access Method Interface Definition<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第51章]]<br />
|-<br />
| Internals<br />
| 52<br />
| GiST Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第52章]]<br />
|-<br />
| Internals<br />
| 53<br />
| GIN Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第53章]]<br />
|-<br />
| Internals<br />
| 54<br />
| Database Physical Storage<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第54章]]<br />
|-<br />
| Internals<br />
| 55<br />
| BKI Backend Interface<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第55章]]<br />
|-<br />
| Internals<br />
| 56<br />
| How the Planner Uses Statistics<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第56章]]<br />
|-<br />
|}<br />
<br />
志愿者简介<br />
<br />
感谢为我们的文档付出了的无私的奉献。<br />
{|border=1<br />
|网名来胳<br />
|猫<br />
|-<br />
|''泥鳅''<br />
|<br />
|-<br />
|''小神厨'' <br />
|anliulin1986 _at_ sina.com<br />
|-<br />
|''Galy''<br />
|galylee _at_ gmail.com<br />
|-<br />
|''hxre''<br />
|hxre _at_ hotmail.com<br />
|-<br />
|''冇得弹''<br />
|zelda_ok _at_ 163.com<br />
|-<br />
|星海游侠<br />
|<br />
|-<br />
|''iihero''<br />
|iihero _at_ qq.com<br />
|}<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AD%E7%AB%A0&diff=208099.1第三十六章2013-09-20T05:02:37Z<p>Horen: /* 一个完整的触发器示例 */</p>
<hr />
<div><br />
'''第36章 触发器'''<br />
----<br />
<br />
这章概括了关于触发器编写的内容。触发器可以用大多数过程语言编写,包括PL/pgSQL (第39章), PL/Tcl (第40章), PL/Perl (第41章), 还有 PL/Python (第42章)。<br />
阅读完本章以后,你可以参照你最喜欢的语言相关的章节,找出用该语言编写触发器的相关细节。<br />
尽管大多数人感觉用过程语言编写触发器相对容易些,但是用C语言同样也可以写出触发器来。<br />
目前还是不能用单纯的SQL语句编写触发器函数。<br />
<br />
== 触发器功能概述 ==<br />
一个触发器是一种声明,告诉数据库应该在执行特定的操作的时候执行特定的函数。触发器可以作用于表和视图。<br />
<br />
作用于表时,触发器可以定义在一个INSERT,UPDATE 或者 DELETE 命令之前或者之后执行,要么是每个记录行被修改,要么是每执行一次 SQL都会触发。UPDATE 触发器还可以设置为UPDATE语句中SET子句包含的特定列发生改动时触发。如果发生触发器事件,那么将在合适的时刻调用触发器函数以处理该事件。<br />
<br />
作用于视图时,触发器不可以定义在 INSERT,UPDATE 或者 DELETE 操作上。而是定义在视图中的每一行需要修改时。触发器的任务是对下层的基础表做必要的修改并返回被修改的行到视图中。作用于视图的触发器还可以被定义为执行INSERT,UPDATE或DELETE操作之前或之后执行一条SQL语句。<br />
<br />
触发器函数必须在创建触发器之前定义。触发器函数必须声明为一个没有参数并且返回trigger类型的函数。(触发器函数通过特殊的TriggerData结构接收其输入,而不是用普通函数参数那种形式。)<br />
<br />
一旦创建了一个合适的触发器函数,触发器就可以用 CREATE TRIGGER 创建。同一个触发器函数可以用于多个触发器。<br />
<br />
PostgreSQL 提供按行触发和按语句触发的触发器。在按行触发的触发器里,在每一行被影响时触发一次。相比之下,一个按语句触发的触发器是在每执行一次合适的语句执行一次的,而不管影响的行数。特别是,一个影响零行的语句将仍然导致任何适用的按语句触发的触发器的执行。这两种类型的触发器有时候分别叫做行级别的触发器和语句级别的触发器。作用于TRUNCATE的触发器只能是语句级别的。对于视图而言,在某个操作之前或之后触发的触发器只能定义为语句级别,但是除了INSERT,UPDATE 或 DELETE之外,只能定义为行级别。<br />
<br />
触发器也可以根据触发时机分类,分别为 BEFORE 触发器、AFTER 触发器和 INSTEAD OF触发器。语句级别的 BEFORE 触发器通常在语句开始做任何事情之前触发,而语句级别的 AFTER 触发器在语句的最后触发。这种类型的触发器可以定义在表和视图上。行级别的 BEFORE 触发器在对特定行进行操作的时候马上触发,而行级别的 AFTER 触发器在语句结束的时候触发(但是在任何语句级别的 AFTER 触发器之前)。这类触发器只能定义在表上。行级别的 INSTEAD OF 触发器只能定义在视图上,并且一旦对视图数据操作时触发。<br />
<br />
语句级别的触发器函数应该总是返回 NULL。如果必要,触发器函数可以给调用它的执行者返回表中的数据行(一个类型为 HeapTuple 的数值)。那些在操作之前触发的行级触发器有以下选择:<br />
<br />
*它可以返回 NULL 以忽略对当前行的操作。这就指示执行器不要执行调用该触发器的行级别操作(对特定行的插入、修改或者删除)。<br />
<br />
*仅对行级别 INSERT 和 UPDATE 触发器而言,其返回将要被插入和更新的行。这样就允许触发器函数修改被插入或者更新的行。<br />
<br />
行级别 BEFORE 触发器并没有这些行为,它返回传进来行(也就是说,对 INSERT 和 UPDATE 触发器来说返回新行,对 DELETE 触发器来说返回被删除的行)。<br />
<br />
行级别 INSTEAD OF 触发器要么返回 NULL 以表示其没修改某视图的底层表,要么返回传进来的视图行(INSERT 和 UPDATE操作的 NEW 行, DELETE操作的 OLD 行)。一个非空的返回值标志着触发器修改了视图的相应数据。这将导致该命令所影响数据行的计数增加。对于 INSERT 和 UPDATE 操作,触发器在返回前可能修改 NEW 行。这将改变被 INSERT RETURNING 或 UPDATE RETURNING 返回的数据,当视图没能展示其数据时非常有用。<br />
<br />
对于在操作之后触发的行级别的触发器,其返回值会被忽略,因此他们可以返回NULL。<br />
<br />
如果多于一个触发器为同样的事件定义在同样的关系上, 触发器将按照由名字的字母顺序排序的顺序触发。 如果是事件之前触发的触发器,每个触发器返回的可能已经被修改过的行成为下一个触发器的输入。 如果任何事件之前触发的触发器返回 NULL 指针, 那么对该行的操作将被丢弃并且随后的触发器不会被触发。<br />
<br />
触发器的定义同样可以指定布尔值的 WHEN 条件,它将用来决定触发器是否应该被触发。行级触发器的 WHEN 条件可以检查该行某列的旧值和新值。(语句级触发器也可以有 WHEN 条件,尽管这个功能没那么有用。)对于BEFORE触发器,WHEN条件在函数执行前判定,所以使用WHEN条件与在函数中判定没有区别。然而,对于AFTER触发器,WHEN条件在行被更新后判定,它决定该事件是否加入被触发队列。所以,如果AFTER触发器WHEN条件没有返回真值,也就没有必要再将该事件放到触发队列,也没必要在语句结束后重新取得行。如果该触发器只定义在少量的行,这个功能对于修改大量行时就行有意义,将大大提升修改速度。INSTEAD OF触发器不支持WHEN条件。<br />
<br />
通常,行级 before 触发器用于检查或修改将要插入或者更新的数据。 比如,一个 before 触发器可以用于把当前时间插入一个 timestamp 字段, 或者跟踪该行的两个元素是否一致。行的 after 触发器多数用于填充或者更新其它表, 或者对其它表进行一致性检查。这么分工的原因是, after 触发器肯定可以看到该行的最后数值, 而 before 触发器不能;还可能有其它的 before 触发器在其后触发。 如果你没有具体的原因定义触发器是 before 还是 after,那么 before 触发器的效率高些, 因为操作相关的信息不必保存到语句的结尾。<br />
<br />
如果一个触发器函数执行 SQL 命令,然后这些命令可能再次触发触发器。 这就是所谓的级联触发器。对级联触发器的级联深度没有明确的限制。 有可能出现级联触发器导致同一个触发器的递归调用的情况; 比如,一个 INSERT 触发器可能执行一个命令, 把一个额外的行插入同一个表中,导致 INSERT 触发器再次激发。 避免这样的无穷递归的问题是触发器程序员的责任。<br />
<br />
在定义一个触发器的时候,我们可以声明一些参数。在触发器定义里面包含参数的目的是允许类似需求的不同触发器调用同一个函数。 比如,我们可能有一个通用的触发器函数,接受两个字段名字,把当前用户放在第一个,而当前时间戳在第二个。 只要我们写得恰当,那么这个触发器函数就可以和触发它的特定表无关。这样同一个函数就可以用于有着合适字段的任何表的 INSERT 事件,实现自动跟踪交易表中的记录创建之类的问题。如果定义成一个 UPDATE 触发器,我们还可以用它跟踪最后更新的事件。<br />
<br />
每种支持触发器的编程语言都有自己的方法让触发器函数得到输入数据。这些输入数据包括触发器事件的类型(比如,INSERT 或者 UPDATE)以及所有在 CREATE TRIGGER 里面列出的参数。对于行级触发器,输入数据也包括 INSERT 和 UPDATE 触发器的 NEW 行,和/或 UPDATE 和 DELETE 触发器的 OLD 行。语句级别的触发器目前没有任何方法检查改语句所修改行。<br />
<br />
== 数据更新的可见性 ==<br />
如果你在你的触发器函数里执行 SQL 命令,并且这些命令访问触发器所对应的表,那么你必须知道触发器的可视性规则,因为这些规则决定这些 SQL 命令是否能看到触发器对应数据的改变。简单说:<br />
<br />
*语句级别的触发器遵循简单的可视性原则:在语句之前(before)触发的触发器看不到所有语句做的修改, 而所有修改都可以被语句之后(after)触发的触发器看到。<br />
<br />
*导致触发器触发的数据改变(插入,更新,或者删除)通常是不能被一个before触发器里面执行的 SQL 命令看到的, 因为它还没有发生。<br />
<br />
*不过,在 before 触发器里执行的 SQL 命令将会看到在同一个外层命令前面处理的行做的数据改变。 这一点需要我们仔细,因为这些改变时间的顺序通常是不可预期的;一个影响多行的 SQL 命令可能以任意顺序访问这些行。<br />
<br />
*同样的,行级INSTEAD OF触发器可以看到之前同一个外层命令的INSTEAD OF触发器影响的数据。<br />
<br />
*在一个 after 触发器被触发的时候,所有外层命令产生的数据改变都已经完成,可以被所执行的 SQL 命令看到。<br />
<br />
如果你的触发器是使用标准过程语言编写的,上面的陈述只适用于函数被声明为VOLATILE. 如果函数被声明为STABLE或者IMMUTABLE,其将看到所有调用命令所有的改变。<br />
<br />
有关数据可视性规则的更多信息可以在 Section 43.4 找到。 Section 36.4 里的例子包含这些规则的演示。<br />
<br />
== 用C语言写触发器 ==<br />
本章描述触发器函数的低层细节。只有当你用 C 书写触发器函数的时候才需要这些信息。如果你用某种高级语言写触发器,那么系统就会为你处理这些细节。在大多数情况下,你在书写自己的 C 触发器之前应该考虑使用过程语言。每种过程语言的文档里面有关于如何用该语言书写触发器的解释。<br />
<br />
触发器函数必须使用"版本 1(version 1)"的函数管理器接口。<br />
<br />
当一个函数被触发器管理器调用时,它不会收到任何普通参数,而是收到一个指向TriggerData结构的"环境"指针。C函数可以执行一个宏来检查是否被触发器管理器调用的:<br />
<br />
<pre>CALLED_AS_TRIGGER(fcinfo)</pre><br />
该宏展开就是:<br />
<br />
<pre>((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))</pre><br />
如果此宏返回真(TRUE),则可以安全地把fcinfo->context转换成类型 TriggerData * 然后使用这个指向 TriggerData 的结构。 函数本身绝不能更改 TriggerData 结构或者它指向的任何数据。<br />
<br />
TriggerData结构体定义在 commands/trigger.h:<br />
<br />
<pre><br />
typedef struct TriggerData<br />
{<br />
NodeTag type;<br />
TriggerEvent tg_event;<br />
Relation tg_relation;<br />
HeapTuple tg_trigtuple;<br />
HeapTuple tg_newtuple;<br />
Trigger *tg_trigger;<br />
Buffer tg_trigtuplebuf;<br />
Buffer tg_newtuplebuf;<br />
} TriggerData;<br />
</pre><br />
where the members are defined as follows:<br />
<br />
type<br />
:总是 T_TriggerData。<br />
<br />
tg_event<br />
:描述调用函数的事件。可以使用tg_event宏来检查:<br />
<br />
:TRIGGER_FIRED_BEFORE(tg_event)<br />
:如果触发器在操作前触发,返回真。<br />
<br />
:TRIGGER_FIRED_AFTER(tg_event)<br />
:如果触发器在操作后触发,返回真。<br />
<br />
:TRIGGER_FIRED_INSTEAD(tg_event)<br />
:如果触发器取代该操作,返回真。<br />
<br />
:TRIGGER_FIRED_FOR_ROW(tg_event)<br />
:如果触发器被行级事件触发,返回真。<br />
<br />
:TRIGGER_FIRED_FOR_STATEMENT(tg_event)<br />
:如果触发器被语句级事件触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_INSERT(tg_event)<br />
:如果触发器被INSERT语句触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_UPDATE(tg_event)<br />
:如果触发器被UPDATE语句触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_DELETE(tg_event)<br />
:如果触发器被DELETE语句触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_TRUNCATE(tg_event)<br />
:如果触发器被TRUNCATE语句触发,返回真。<br />
<br />
tg_relation<br />
:是一个指向描述被触发的关系的结构的指针。 请参考utils/rel.h获取关于此结构的详细信息。 最让人感兴趣的事情是 tg_relation->rd_att(关系元组的描述) 和tg_relation->rd_rel->relname(关系名。这个变量的类型不是 char*,而是NameData。 如果你需要一份名字的字符串拷贝,用 SPI_getrelname(tg_relation)获取)。<br />
<br />
tg_trigtuple<br />
:是一个指向触发触发器的行的指针。这是一个正在被插入(INSERT), 删除(DELETE)或更新(UPDATE)的行。如果是 INSERT或DELETE, 那么这就是你将返回给执行者的东西---如果你不想用另一条行覆盖此行(INSERT)或忽略操作(在DELETE的时候)。<br />
<br />
tg_newtuple<br />
:如果是UPDATE,这是一个指向新版本的行的指针,如果是INSERT 或DELETE, 就是NULL。 这就是你将返回给执行者的东西---如果事件是 UPDATE 并且你不想用另一条行替换这条行或忽略操作。<br />
<br />
tg_trigger<br />
:是一个指向结构Trigger的指针,该结构在utils/rel.h里定义:<br />
<br />
<pre>typedef struct Trigger<br />
{<br />
Oid tgoid;<br />
char *tgname;<br />
Oid tgfoid;<br />
int16 tgtype;<br />
char tgenabled;<br />
bool tgisinternal;<br />
Oid tgconstrrelid;<br />
Oid tgconstrindid;<br />
Oid tgconstraint;<br />
bool tgdeferrable;<br />
bool tginitdeferred;<br />
int16 tgnargs;<br />
int16 tgnattr;<br />
int16 *tgattr;<br />
char **tgargs;<br />
char *tgqual;<br />
} Trigger;<br />
</pre><br />
:tgname是触发器的名称,tgnargs 是在tgargs里参数的数量,tgargs是一个指针数组,数组里每个指针指向在 CREATE TRIGGER语句里声明的参数。其他成员只在内部使用。<br />
<br />
tg_trigtuplebuf<br />
:如果没有元组或者没有存储在磁盘缓冲区里,则是包含 tg_trigtuple 或者 InvalidBuffer 的缓冲区。<br />
<br />
tg_newtuplebuf<br />
:如果没有元组或者它并未存储在磁盘缓冲区里,那么就是包含 tg_newtuple, 或者 InvalidBuffer 的缓冲区。<br />
<br />
一个触发器函数必须返回一个HeapTuple 指针或者一个 NULL 指针(不是SQL NULL,也就是说,不要设置 isNull 为真)。请注意如果你不想修改正在被操作的行,那么要根据情况返回 tg_trigtuple 或者 tg_newtuple。<br />
<br />
== 一个完整的触发器示例 ==<br />
这里是一个用 C 写的非常简单的触发器使用的例子。(使用过程语言编写触发器的例子可以在过程语言文档中找到。)<br />
<br />
函数trigf报告表 ttest 中行数量,并且如果命令试图把空值插入到字段 x 里(也就是说-它做为一个非空约束但不退出事务的约束)时略过操作。 <br />
<br />
首先定义这张表:<br />
<br />
<pre><br />
CREATE TABLE ttest (<br />
x integer<br />
);</pre><br />
下面是触发器函数的代码:<br />
<br />
<pre><br />
#include "postgres.h"<br />
#include "executor/spi.h" /* this is what you need to work with SPI */<br />
#include "commands/trigger.h" /* ... and triggers */<br />
<br />
#ifdef PG_MODULE_MAGIC<br />
PG_MODULE_MAGIC;<br />
#endif<br />
<br />
extern Datum trigf(PG_FUNCTION_ARGS);<br />
<br />
PG_FUNCTION_INFO_V1(trigf);<br />
<br />
Datum<br />
trigf(PG_FUNCTION_ARGS)<br />
{<br />
TriggerData *trigdata = (TriggerData *) fcinfo->context;<br />
TupleDesc tupdesc;<br />
HeapTuple rettuple;<br />
char *when;<br />
bool checknull = false;<br />
bool isnull;<br />
int ret, i;<br />
<br />
/* make sure it's called as a trigger at all */<br />
if (!CALLED_AS_TRIGGER(fcinfo))<br />
elog(ERROR, "trigf: not called by trigger manager");<br />
<br />
/* tuple to return to executor */<br />
if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))<br />
rettuple = trigdata->tg_newtuple;<br />
else<br />
rettuple = trigdata->tg_trigtuple;<br />
<br />
/* check for null values */<br />
if (!TRIGGER_FIRED_BY_DELETE(trigdata->tg_event)<br />
&& TRIGGER_FIRED_BEFORE(trigdata->tg_event))<br />
checknull = true;<br />
<br />
if (TRIGGER_FIRED_BEFORE(trigdata->tg_event))<br />
when = "before";<br />
else<br />
when = "after ";<br />
<br />
tupdesc = trigdata->tg_relation->rd_att;<br />
<br />
/* connect to SPI manager */<br />
if ((ret = SPI_connect()) < 0)<br />
elog(ERROR, "trigf (fired %s): SPI_connect returned %d", when, ret);<br />
<br />
/* get number of rows in table */<br />
ret = SPI_exec("SELECT count(*) FROM ttest", 0);<br />
<br />
if (ret < 0)<br />
elog(ERROR, "trigf (fired %s): SPI_exec returned %d", when, ret);<br />
<br />
/* count(*) returns int8, so be careful to convert */<br />
i = DatumGetInt64(SPI_getbinval(SPI_tuptable->vals[0],<br />
SPI_tuptable->tupdesc,<br />
1,<br />
&isnull));<br />
<br />
elog (INFO, "trigf (fired %s): there are %d rows in ttest", when, i);<br />
<br />
SPI_finish();<br />
<br />
if (checknull)<br />
{<br />
SPI_getbinval(rettuple, tupdesc, 1, &isnull);<br />
if (isnull)<br />
rettuple = NULL;<br />
}<br />
<br />
return PointerGetDatum(rettuple);<br />
}<br />
</pre><br />
编译完源码后(参看 Section 35.9.6),声明函数和触发器:<br />
<br />
<pre><br />
CREATE FUNCTION trigf() RETURNS trigger<br />
AS 'filename'<br />
LANGUAGE C;<br />
<br />
CREATE TRIGGER tbefore BEFORE INSERT OR UPDATE OR DELETE ON ttest<br />
FOR EACH ROW EXECUTE PROCEDURE trigf();<br />
<br />
CREATE TRIGGER tafter AFTER INSERT OR UPDATE OR DELETE ON ttest<br />
FOR EACH ROW EXECUTE PROCEDURE trigf();<br />
</pre><br />
<br />
现在你可以测试触发器的操作:<br />
<br />
<pre><br />
=> INSERT INTO ttest VALUES (NULL);<br />
INFO: trigf (fired before): there are 0 rows in ttest<br />
INSERT 0 0<br />
<br />
-- Insertion skipped and AFTER trigger is not fired<br />
<br />
=> SELECT * FROM ttest;<br />
x<br />
---<br />
(0 rows)<br />
<br />
=> INSERT INTO ttest VALUES (1);<br />
INFO: trigf (fired before): there are 0 rows in ttest<br />
INFO: trigf (fired after ): there are 1 rows in ttest<br />
^^^^^^^^<br />
remember what we said about visibility.<br />
INSERT 167793 1<br />
vac=> SELECT * FROM ttest;<br />
x<br />
---<br />
1<br />
(1 row)<br />
<br />
=> INSERT INTO ttest SELECT x * 2 FROM ttest;<br />
INFO: trigf (fired before): there are 1 rows in ttest<br />
INFO: trigf (fired after ): there are 2 rows in ttest<br />
^^^^^^<br />
remember what we said about visibility.<br />
INSERT 167794 1<br />
=> SELECT * FROM ttest;<br />
x<br />
---<br />
1<br />
2<br />
(2 rows)<br />
<br />
=> UPDATE ttest SET x = NULL WHERE x = 2;<br />
INFO: trigf (fired before): there are 2 rows in ttest<br />
UPDATE 0<br />
=> UPDATE ttest SET x = 4 WHERE x = 2;<br />
INFO: trigf (fired before): there are 2 rows in ttest<br />
INFO: trigf (fired after ): there are 2 rows in ttest<br />
UPDATE 1<br />
vac=> SELECT * FROM ttest;<br />
x<br />
---<br />
1<br />
4<br />
(2 rows)<br />
<br />
=> DELETE FROM ttest;<br />
INFO: trigf (fired before): there are 2 rows in ttest<br />
INFO: trigf (fired before): there are 1 rows in ttest<br />
INFO: trigf (fired after ): there are 0 rows in ttest<br />
INFO: trigf (fired after ): there are 0 rows in ttest<br />
^^^^^^<br />
remember what we said about visibility.<br />
DELETE 2<br />
=> SELECT * FROM ttest;<br />
x<br />
---<br />
(0 rows)<br />
</pre><br />
在src/test/regress/regress.c和spi里还有更复杂的操作。</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AD%E7%AB%A0&diff=208089.1第三十六章2013-09-20T04:42:37Z<p>Horen: /* 用C语言写触发器 */</p>
<hr />
<div><br />
'''第36章 触发器'''<br />
----<br />
<br />
这章概括了关于触发器编写的内容。触发器可以用大多数过程语言编写,包括PL/pgSQL (第39章), PL/Tcl (第40章), PL/Perl (第41章), 还有 PL/Python (第42章)。<br />
阅读完本章以后,你可以参照你最喜欢的语言相关的章节,找出用该语言编写触发器的相关细节。<br />
尽管大多数人感觉用过程语言编写触发器相对容易些,但是用C语言同样也可以写出触发器来。<br />
目前还是不能用单纯的SQL语句编写触发器函数。<br />
<br />
== 触发器功能概述 ==<br />
一个触发器是一种声明,告诉数据库应该在执行特定的操作的时候执行特定的函数。触发器可以作用于表和视图。<br />
<br />
作用于表时,触发器可以定义在一个INSERT,UPDATE 或者 DELETE 命令之前或者之后执行,要么是每个记录行被修改,要么是每执行一次 SQL都会触发。UPDATE 触发器还可以设置为UPDATE语句中SET子句包含的特定列发生改动时触发。如果发生触发器事件,那么将在合适的时刻调用触发器函数以处理该事件。<br />
<br />
作用于视图时,触发器不可以定义在 INSERT,UPDATE 或者 DELETE 操作上。而是定义在视图中的每一行需要修改时。触发器的任务是对下层的基础表做必要的修改并返回被修改的行到视图中。作用于视图的触发器还可以被定义为执行INSERT,UPDATE或DELETE操作之前或之后执行一条SQL语句。<br />
<br />
触发器函数必须在创建触发器之前定义。触发器函数必须声明为一个没有参数并且返回trigger类型的函数。(触发器函数通过特殊的TriggerData结构接收其输入,而不是用普通函数参数那种形式。)<br />
<br />
一旦创建了一个合适的触发器函数,触发器就可以用 CREATE TRIGGER 创建。同一个触发器函数可以用于多个触发器。<br />
<br />
PostgreSQL 提供按行触发和按语句触发的触发器。在按行触发的触发器里,在每一行被影响时触发一次。相比之下,一个按语句触发的触发器是在每执行一次合适的语句执行一次的,而不管影响的行数。特别是,一个影响零行的语句将仍然导致任何适用的按语句触发的触发器的执行。这两种类型的触发器有时候分别叫做行级别的触发器和语句级别的触发器。作用于TRUNCATE的触发器只能是语句级别的。对于视图而言,在某个操作之前或之后触发的触发器只能定义为语句级别,但是除了INSERT,UPDATE 或 DELETE之外,只能定义为行级别。<br />
<br />
触发器也可以根据触发时机分类,分别为 BEFORE 触发器、AFTER 触发器和 INSTEAD OF触发器。语句级别的 BEFORE 触发器通常在语句开始做任何事情之前触发,而语句级别的 AFTER 触发器在语句的最后触发。这种类型的触发器可以定义在表和视图上。行级别的 BEFORE 触发器在对特定行进行操作的时候马上触发,而行级别的 AFTER 触发器在语句结束的时候触发(但是在任何语句级别的 AFTER 触发器之前)。这类触发器只能定义在表上。行级别的 INSTEAD OF 触发器只能定义在视图上,并且一旦对视图数据操作时触发。<br />
<br />
语句级别的触发器函数应该总是返回 NULL。如果必要,触发器函数可以给调用它的执行者返回表中的数据行(一个类型为 HeapTuple 的数值)。那些在操作之前触发的行级触发器有以下选择:<br />
<br />
*它可以返回 NULL 以忽略对当前行的操作。这就指示执行器不要执行调用该触发器的行级别操作(对特定行的插入、修改或者删除)。<br />
<br />
*仅对行级别 INSERT 和 UPDATE 触发器而言,其返回将要被插入和更新的行。这样就允许触发器函数修改被插入或者更新的行。<br />
<br />
行级别 BEFORE 触发器并没有这些行为,它返回传进来行(也就是说,对 INSERT 和 UPDATE 触发器来说返回新行,对 DELETE 触发器来说返回被删除的行)。<br />
<br />
行级别 INSTEAD OF 触发器要么返回 NULL 以表示其没修改某视图的底层表,要么返回传进来的视图行(INSERT 和 UPDATE操作的 NEW 行, DELETE操作的 OLD 行)。一个非空的返回值标志着触发器修改了视图的相应数据。这将导致该命令所影响数据行的计数增加。对于 INSERT 和 UPDATE 操作,触发器在返回前可能修改 NEW 行。这将改变被 INSERT RETURNING 或 UPDATE RETURNING 返回的数据,当视图没能展示其数据时非常有用。<br />
<br />
对于在操作之后触发的行级别的触发器,其返回值会被忽略,因此他们可以返回NULL。<br />
<br />
如果多于一个触发器为同样的事件定义在同样的关系上, 触发器将按照由名字的字母顺序排序的顺序触发。 如果是事件之前触发的触发器,每个触发器返回的可能已经被修改过的行成为下一个触发器的输入。 如果任何事件之前触发的触发器返回 NULL 指针, 那么对该行的操作将被丢弃并且随后的触发器不会被触发。<br />
<br />
触发器的定义同样可以指定布尔值的 WHEN 条件,它将用来决定触发器是否应该被触发。行级触发器的 WHEN 条件可以检查该行某列的旧值和新值。(语句级触发器也可以有 WHEN 条件,尽管这个功能没那么有用。)对于BEFORE触发器,WHEN条件在函数执行前判定,所以使用WHEN条件与在函数中判定没有区别。然而,对于AFTER触发器,WHEN条件在行被更新后判定,它决定该事件是否加入被触发队列。所以,如果AFTER触发器WHEN条件没有返回真值,也就没有必要再将该事件放到触发队列,也没必要在语句结束后重新取得行。如果该触发器只定义在少量的行,这个功能对于修改大量行时就行有意义,将大大提升修改速度。INSTEAD OF触发器不支持WHEN条件。<br />
<br />
通常,行级 before 触发器用于检查或修改将要插入或者更新的数据。 比如,一个 before 触发器可以用于把当前时间插入一个 timestamp 字段, 或者跟踪该行的两个元素是否一致。行的 after 触发器多数用于填充或者更新其它表, 或者对其它表进行一致性检查。这么分工的原因是, after 触发器肯定可以看到该行的最后数值, 而 before 触发器不能;还可能有其它的 before 触发器在其后触发。 如果你没有具体的原因定义触发器是 before 还是 after,那么 before 触发器的效率高些, 因为操作相关的信息不必保存到语句的结尾。<br />
<br />
如果一个触发器函数执行 SQL 命令,然后这些命令可能再次触发触发器。 这就是所谓的级联触发器。对级联触发器的级联深度没有明确的限制。 有可能出现级联触发器导致同一个触发器的递归调用的情况; 比如,一个 INSERT 触发器可能执行一个命令, 把一个额外的行插入同一个表中,导致 INSERT 触发器再次激发。 避免这样的无穷递归的问题是触发器程序员的责任。<br />
<br />
在定义一个触发器的时候,我们可以声明一些参数。在触发器定义里面包含参数的目的是允许类似需求的不同触发器调用同一个函数。 比如,我们可能有一个通用的触发器函数,接受两个字段名字,把当前用户放在第一个,而当前时间戳在第二个。 只要我们写得恰当,那么这个触发器函数就可以和触发它的特定表无关。这样同一个函数就可以用于有着合适字段的任何表的 INSERT 事件,实现自动跟踪交易表中的记录创建之类的问题。如果定义成一个 UPDATE 触发器,我们还可以用它跟踪最后更新的事件。<br />
<br />
每种支持触发器的编程语言都有自己的方法让触发器函数得到输入数据。这些输入数据包括触发器事件的类型(比如,INSERT 或者 UPDATE)以及所有在 CREATE TRIGGER 里面列出的参数。对于行级触发器,输入数据也包括 INSERT 和 UPDATE 触发器的 NEW 行,和/或 UPDATE 和 DELETE 触发器的 OLD 行。语句级别的触发器目前没有任何方法检查改语句所修改行。<br />
<br />
== 数据更新的可见性 ==<br />
如果你在你的触发器函数里执行 SQL 命令,并且这些命令访问触发器所对应的表,那么你必须知道触发器的可视性规则,因为这些规则决定这些 SQL 命令是否能看到触发器对应数据的改变。简单说:<br />
<br />
*语句级别的触发器遵循简单的可视性原则:在语句之前(before)触发的触发器看不到所有语句做的修改, 而所有修改都可以被语句之后(after)触发的触发器看到。<br />
<br />
*导致触发器触发的数据改变(插入,更新,或者删除)通常是不能被一个before触发器里面执行的 SQL 命令看到的, 因为它还没有发生。<br />
<br />
*不过,在 before 触发器里执行的 SQL 命令将会看到在同一个外层命令前面处理的行做的数据改变。 这一点需要我们仔细,因为这些改变时间的顺序通常是不可预期的;一个影响多行的 SQL 命令可能以任意顺序访问这些行。<br />
<br />
*同样的,行级INSTEAD OF触发器可以看到之前同一个外层命令的INSTEAD OF触发器影响的数据。<br />
<br />
*在一个 after 触发器被触发的时候,所有外层命令产生的数据改变都已经完成,可以被所执行的 SQL 命令看到。<br />
<br />
如果你的触发器是使用标准过程语言编写的,上面的陈述只适用于函数被声明为VOLATILE. 如果函数被声明为STABLE或者IMMUTABLE,其将看到所有调用命令所有的改变。<br />
<br />
有关数据可视性规则的更多信息可以在 Section 43.4 找到。 Section 36.4 里的例子包含这些规则的演示。<br />
<br />
== 用C语言写触发器 ==<br />
本章描述触发器函数的低层细节。只有当你用 C 书写触发器函数的时候才需要这些信息。如果你用某种高级语言写触发器,那么系统就会为你处理这些细节。在大多数情况下,你在书写自己的 C 触发器之前应该考虑使用过程语言。每种过程语言的文档里面有关于如何用该语言书写触发器的解释。<br />
<br />
触发器函数必须使用"版本 1(version 1)"的函数管理器接口。<br />
<br />
当一个函数被触发器管理器调用时,它不会收到任何普通参数,而是收到一个指向TriggerData结构的"环境"指针。C函数可以执行一个宏来检查是否被触发器管理器调用的:<br />
<br />
<pre>CALLED_AS_TRIGGER(fcinfo)</pre><br />
该宏展开就是:<br />
<br />
<pre>((fcinfo)->context != NULL && IsA((fcinfo)->context, TriggerData))</pre><br />
如果此宏返回真(TRUE),则可以安全地把fcinfo->context转换成类型 TriggerData * 然后使用这个指向 TriggerData 的结构。 函数本身绝不能更改 TriggerData 结构或者它指向的任何数据。<br />
<br />
TriggerData结构体定义在 commands/trigger.h:<br />
<br />
<pre><br />
typedef struct TriggerData<br />
{<br />
NodeTag type;<br />
TriggerEvent tg_event;<br />
Relation tg_relation;<br />
HeapTuple tg_trigtuple;<br />
HeapTuple tg_newtuple;<br />
Trigger *tg_trigger;<br />
Buffer tg_trigtuplebuf;<br />
Buffer tg_newtuplebuf;<br />
} TriggerData;<br />
</pre><br />
where the members are defined as follows:<br />
<br />
type<br />
:总是 T_TriggerData。<br />
<br />
tg_event<br />
:描述调用函数的事件。可以使用tg_event宏来检查:<br />
<br />
:TRIGGER_FIRED_BEFORE(tg_event)<br />
:如果触发器在操作前触发,返回真。<br />
<br />
:TRIGGER_FIRED_AFTER(tg_event)<br />
:如果触发器在操作后触发,返回真。<br />
<br />
:TRIGGER_FIRED_INSTEAD(tg_event)<br />
:如果触发器取代该操作,返回真。<br />
<br />
:TRIGGER_FIRED_FOR_ROW(tg_event)<br />
:如果触发器被行级事件触发,返回真。<br />
<br />
:TRIGGER_FIRED_FOR_STATEMENT(tg_event)<br />
:如果触发器被语句级事件触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_INSERT(tg_event)<br />
:如果触发器被INSERT语句触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_UPDATE(tg_event)<br />
:如果触发器被UPDATE语句触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_DELETE(tg_event)<br />
:如果触发器被DELETE语句触发,返回真。<br />
<br />
:TRIGGER_FIRED_BY_TRUNCATE(tg_event)<br />
:如果触发器被TRUNCATE语句触发,返回真。<br />
<br />
tg_relation<br />
:是一个指向描述被触发的关系的结构的指针。 请参考utils/rel.h获取关于此结构的详细信息。 最让人感兴趣的事情是 tg_relation->rd_att(关系元组的描述) 和tg_relation->rd_rel->relname(关系名。这个变量的类型不是 char*,而是NameData。 如果你需要一份名字的字符串拷贝,用 SPI_getrelname(tg_relation)获取)。<br />
<br />
tg_trigtuple<br />
:是一个指向触发触发器的行的指针。这是一个正在被插入(INSERT), 删除(DELETE)或更新(UPDATE)的行。如果是 INSERT或DELETE, 那么这就是你将返回给执行者的东西---如果你不想用另一条行覆盖此行(INSERT)或忽略操作(在DELETE的时候)。<br />
<br />
tg_newtuple<br />
:如果是UPDATE,这是一个指向新版本的行的指针,如果是INSERT 或DELETE, 就是NULL。 这就是你将返回给执行者的东西---如果事件是 UPDATE 并且你不想用另一条行替换这条行或忽略操作。<br />
<br />
tg_trigger<br />
:是一个指向结构Trigger的指针,该结构在utils/rel.h里定义:<br />
<br />
<pre>typedef struct Trigger<br />
{<br />
Oid tgoid;<br />
char *tgname;<br />
Oid tgfoid;<br />
int16 tgtype;<br />
char tgenabled;<br />
bool tgisinternal;<br />
Oid tgconstrrelid;<br />
Oid tgconstrindid;<br />
Oid tgconstraint;<br />
bool tgdeferrable;<br />
bool tginitdeferred;<br />
int16 tgnargs;<br />
int16 tgnattr;<br />
int16 *tgattr;<br />
char **tgargs;<br />
char *tgqual;<br />
} Trigger;<br />
</pre><br />
:tgname是触发器的名称,tgnargs 是在tgargs里参数的数量,tgargs是一个指针数组,数组里每个指针指向在 CREATE TRIGGER语句里声明的参数。其他成员只在内部使用。<br />
<br />
tg_trigtuplebuf<br />
:如果没有元组或者没有存储在磁盘缓冲区里,则是包含 tg_trigtuple 或者 InvalidBuffer 的缓冲区。<br />
<br />
tg_newtuplebuf<br />
:如果没有元组或者它并未存储在磁盘缓冲区里,那么就是包含 tg_newtuple, 或者 InvalidBuffer 的缓冲区。<br />
<br />
一个触发器函数必须返回一个HeapTuple 指针或者一个 NULL 指针(不是SQL NULL,也就是说,不要设置 isNull 为真)。请注意如果你不想修改正在被操作的行,那么要根据情况返回 tg_trigtuple 或者 tg_newtuple。<br />
<br />
== 一个完整的触发器示例 ==<br />
<br />
<br />
<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AD%E7%AB%A0&diff=208079.1第三十六章2013-09-20T04:16:49Z<p>Horen: /* 数据更新的可见性 */</p>
<hr />
<div><br />
'''第36章 触发器'''<br />
----<br />
<br />
这章概括了关于触发器编写的内容。触发器可以用大多数过程语言编写,包括PL/pgSQL (第39章), PL/Tcl (第40章), PL/Perl (第41章), 还有 PL/Python (第42章)。<br />
阅读完本章以后,你可以参照你最喜欢的语言相关的章节,找出用该语言编写触发器的相关细节。<br />
尽管大多数人感觉用过程语言编写触发器相对容易些,但是用C语言同样也可以写出触发器来。<br />
目前还是不能用单纯的SQL语句编写触发器函数。<br />
<br />
== 触发器功能概述 ==<br />
一个触发器是一种声明,告诉数据库应该在执行特定的操作的时候执行特定的函数。触发器可以作用于表和视图。<br />
<br />
作用于表时,触发器可以定义在一个INSERT,UPDATE 或者 DELETE 命令之前或者之后执行,要么是每个记录行被修改,要么是每执行一次 SQL都会触发。UPDATE 触发器还可以设置为UPDATE语句中SET子句包含的特定列发生改动时触发。如果发生触发器事件,那么将在合适的时刻调用触发器函数以处理该事件。<br />
<br />
作用于视图时,触发器不可以定义在 INSERT,UPDATE 或者 DELETE 操作上。而是定义在视图中的每一行需要修改时。触发器的任务是对下层的基础表做必要的修改并返回被修改的行到视图中。作用于视图的触发器还可以被定义为执行INSERT,UPDATE或DELETE操作之前或之后执行一条SQL语句。<br />
<br />
触发器函数必须在创建触发器之前定义。触发器函数必须声明为一个没有参数并且返回trigger类型的函数。(触发器函数通过特殊的TriggerData结构接收其输入,而不是用普通函数参数那种形式。)<br />
<br />
一旦创建了一个合适的触发器函数,触发器就可以用 CREATE TRIGGER 创建。同一个触发器函数可以用于多个触发器。<br />
<br />
PostgreSQL 提供按行触发和按语句触发的触发器。在按行触发的触发器里,在每一行被影响时触发一次。相比之下,一个按语句触发的触发器是在每执行一次合适的语句执行一次的,而不管影响的行数。特别是,一个影响零行的语句将仍然导致任何适用的按语句触发的触发器的执行。这两种类型的触发器有时候分别叫做行级别的触发器和语句级别的触发器。作用于TRUNCATE的触发器只能是语句级别的。对于视图而言,在某个操作之前或之后触发的触发器只能定义为语句级别,但是除了INSERT,UPDATE 或 DELETE之外,只能定义为行级别。<br />
<br />
触发器也可以根据触发时机分类,分别为 BEFORE 触发器、AFTER 触发器和 INSTEAD OF触发器。语句级别的 BEFORE 触发器通常在语句开始做任何事情之前触发,而语句级别的 AFTER 触发器在语句的最后触发。这种类型的触发器可以定义在表和视图上。行级别的 BEFORE 触发器在对特定行进行操作的时候马上触发,而行级别的 AFTER 触发器在语句结束的时候触发(但是在任何语句级别的 AFTER 触发器之前)。这类触发器只能定义在表上。行级别的 INSTEAD OF 触发器只能定义在视图上,并且一旦对视图数据操作时触发。<br />
<br />
语句级别的触发器函数应该总是返回 NULL。如果必要,触发器函数可以给调用它的执行者返回表中的数据行(一个类型为 HeapTuple 的数值)。那些在操作之前触发的行级触发器有以下选择:<br />
<br />
*它可以返回 NULL 以忽略对当前行的操作。这就指示执行器不要执行调用该触发器的行级别操作(对特定行的插入、修改或者删除)。<br />
<br />
*仅对行级别 INSERT 和 UPDATE 触发器而言,其返回将要被插入和更新的行。这样就允许触发器函数修改被插入或者更新的行。<br />
<br />
行级别 BEFORE 触发器并没有这些行为,它返回传进来行(也就是说,对 INSERT 和 UPDATE 触发器来说返回新行,对 DELETE 触发器来说返回被删除的行)。<br />
<br />
行级别 INSTEAD OF 触发器要么返回 NULL 以表示其没修改某视图的底层表,要么返回传进来的视图行(INSERT 和 UPDATE操作的 NEW 行, DELETE操作的 OLD 行)。一个非空的返回值标志着触发器修改了视图的相应数据。这将导致该命令所影响数据行的计数增加。对于 INSERT 和 UPDATE 操作,触发器在返回前可能修改 NEW 行。这将改变被 INSERT RETURNING 或 UPDATE RETURNING 返回的数据,当视图没能展示其数据时非常有用。<br />
<br />
对于在操作之后触发的行级别的触发器,其返回值会被忽略,因此他们可以返回NULL。<br />
<br />
如果多于一个触发器为同样的事件定义在同样的关系上, 触发器将按照由名字的字母顺序排序的顺序触发。 如果是事件之前触发的触发器,每个触发器返回的可能已经被修改过的行成为下一个触发器的输入。 如果任何事件之前触发的触发器返回 NULL 指针, 那么对该行的操作将被丢弃并且随后的触发器不会被触发。<br />
<br />
触发器的定义同样可以指定布尔值的 WHEN 条件,它将用来决定触发器是否应该被触发。行级触发器的 WHEN 条件可以检查该行某列的旧值和新值。(语句级触发器也可以有 WHEN 条件,尽管这个功能没那么有用。)对于BEFORE触发器,WHEN条件在函数执行前判定,所以使用WHEN条件与在函数中判定没有区别。然而,对于AFTER触发器,WHEN条件在行被更新后判定,它决定该事件是否加入被触发队列。所以,如果AFTER触发器WHEN条件没有返回真值,也就没有必要再将该事件放到触发队列,也没必要在语句结束后重新取得行。如果该触发器只定义在少量的行,这个功能对于修改大量行时就行有意义,将大大提升修改速度。INSTEAD OF触发器不支持WHEN条件。<br />
<br />
通常,行级 before 触发器用于检查或修改将要插入或者更新的数据。 比如,一个 before 触发器可以用于把当前时间插入一个 timestamp 字段, 或者跟踪该行的两个元素是否一致。行的 after 触发器多数用于填充或者更新其它表, 或者对其它表进行一致性检查。这么分工的原因是, after 触发器肯定可以看到该行的最后数值, 而 before 触发器不能;还可能有其它的 before 触发器在其后触发。 如果你没有具体的原因定义触发器是 before 还是 after,那么 before 触发器的效率高些, 因为操作相关的信息不必保存到语句的结尾。<br />
<br />
如果一个触发器函数执行 SQL 命令,然后这些命令可能再次触发触发器。 这就是所谓的级联触发器。对级联触发器的级联深度没有明确的限制。 有可能出现级联触发器导致同一个触发器的递归调用的情况; 比如,一个 INSERT 触发器可能执行一个命令, 把一个额外的行插入同一个表中,导致 INSERT 触发器再次激发。 避免这样的无穷递归的问题是触发器程序员的责任。<br />
<br />
在定义一个触发器的时候,我们可以声明一些参数。在触发器定义里面包含参数的目的是允许类似需求的不同触发器调用同一个函数。 比如,我们可能有一个通用的触发器函数,接受两个字段名字,把当前用户放在第一个,而当前时间戳在第二个。 只要我们写得恰当,那么这个触发器函数就可以和触发它的特定表无关。这样同一个函数就可以用于有着合适字段的任何表的 INSERT 事件,实现自动跟踪交易表中的记录创建之类的问题。如果定义成一个 UPDATE 触发器,我们还可以用它跟踪最后更新的事件。<br />
<br />
每种支持触发器的编程语言都有自己的方法让触发器函数得到输入数据。这些输入数据包括触发器事件的类型(比如,INSERT 或者 UPDATE)以及所有在 CREATE TRIGGER 里面列出的参数。对于行级触发器,输入数据也包括 INSERT 和 UPDATE 触发器的 NEW 行,和/或 UPDATE 和 DELETE 触发器的 OLD 行。语句级别的触发器目前没有任何方法检查改语句所修改行。<br />
<br />
== 数据更新的可见性 ==<br />
如果你在你的触发器函数里执行 SQL 命令,并且这些命令访问触发器所对应的表,那么你必须知道触发器的可视性规则,因为这些规则决定这些 SQL 命令是否能看到触发器对应数据的改变。简单说:<br />
<br />
*语句级别的触发器遵循简单的可视性原则:在语句之前(before)触发的触发器看不到所有语句做的修改, 而所有修改都可以被语句之后(after)触发的触发器看到。<br />
<br />
*导致触发器触发的数据改变(插入,更新,或者删除)通常是不能被一个before触发器里面执行的 SQL 命令看到的, 因为它还没有发生。<br />
<br />
*不过,在 before 触发器里执行的 SQL 命令将会看到在同一个外层命令前面处理的行做的数据改变。 这一点需要我们仔细,因为这些改变时间的顺序通常是不可预期的;一个影响多行的 SQL 命令可能以任意顺序访问这些行。<br />
<br />
*同样的,行级INSTEAD OF触发器可以看到之前同一个外层命令的INSTEAD OF触发器影响的数据。<br />
<br />
*在一个 after 触发器被触发的时候,所有外层命令产生的数据改变都已经完成,可以被所执行的 SQL 命令看到。<br />
<br />
如果你的触发器是使用标准过程语言编写的,上面的陈述只适用于函数被声明为VOLATILE. 如果函数被声明为STABLE或者IMMUTABLE,其将看到所有调用命令所有的改变。<br />
<br />
有关数据可视性规则的更多信息可以在 Section 43.4 找到。 Section 36.4 里的例子包含这些规则的演示。<br />
<br />
== 用C语言写触发器 ==<br />
== 一个完整的触发器示例 ==<br />
<br />
<br />
<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AD%E7%AB%A0&diff=208069.1第三十六章2013-09-20T04:11:36Z<p>Horen: /* 触发器功能概述 */</p>
<hr />
<div><br />
'''第36章 触发器'''<br />
----<br />
<br />
这章概括了关于触发器编写的内容。触发器可以用大多数过程语言编写,包括PL/pgSQL (第39章), PL/Tcl (第40章), PL/Perl (第41章), 还有 PL/Python (第42章)。<br />
阅读完本章以后,你可以参照你最喜欢的语言相关的章节,找出用该语言编写触发器的相关细节。<br />
尽管大多数人感觉用过程语言编写触发器相对容易些,但是用C语言同样也可以写出触发器来。<br />
目前还是不能用单纯的SQL语句编写触发器函数。<br />
<br />
== 触发器功能概述 ==<br />
一个触发器是一种声明,告诉数据库应该在执行特定的操作的时候执行特定的函数。触发器可以作用于表和视图。<br />
<br />
作用于表时,触发器可以定义在一个INSERT,UPDATE 或者 DELETE 命令之前或者之后执行,要么是每个记录行被修改,要么是每执行一次 SQL都会触发。UPDATE 触发器还可以设置为UPDATE语句中SET子句包含的特定列发生改动时触发。如果发生触发器事件,那么将在合适的时刻调用触发器函数以处理该事件。<br />
<br />
作用于视图时,触发器不可以定义在 INSERT,UPDATE 或者 DELETE 操作上。而是定义在视图中的每一行需要修改时。触发器的任务是对下层的基础表做必要的修改并返回被修改的行到视图中。作用于视图的触发器还可以被定义为执行INSERT,UPDATE或DELETE操作之前或之后执行一条SQL语句。<br />
<br />
触发器函数必须在创建触发器之前定义。触发器函数必须声明为一个没有参数并且返回trigger类型的函数。(触发器函数通过特殊的TriggerData结构接收其输入,而不是用普通函数参数那种形式。)<br />
<br />
一旦创建了一个合适的触发器函数,触发器就可以用 CREATE TRIGGER 创建。同一个触发器函数可以用于多个触发器。<br />
<br />
PostgreSQL 提供按行触发和按语句触发的触发器。在按行触发的触发器里,在每一行被影响时触发一次。相比之下,一个按语句触发的触发器是在每执行一次合适的语句执行一次的,而不管影响的行数。特别是,一个影响零行的语句将仍然导致任何适用的按语句触发的触发器的执行。这两种类型的触发器有时候分别叫做行级别的触发器和语句级别的触发器。作用于TRUNCATE的触发器只能是语句级别的。对于视图而言,在某个操作之前或之后触发的触发器只能定义为语句级别,但是除了INSERT,UPDATE 或 DELETE之外,只能定义为行级别。<br />
<br />
触发器也可以根据触发时机分类,分别为 BEFORE 触发器、AFTER 触发器和 INSTEAD OF触发器。语句级别的 BEFORE 触发器通常在语句开始做任何事情之前触发,而语句级别的 AFTER 触发器在语句的最后触发。这种类型的触发器可以定义在表和视图上。行级别的 BEFORE 触发器在对特定行进行操作的时候马上触发,而行级别的 AFTER 触发器在语句结束的时候触发(但是在任何语句级别的 AFTER 触发器之前)。这类触发器只能定义在表上。行级别的 INSTEAD OF 触发器只能定义在视图上,并且一旦对视图数据操作时触发。<br />
<br />
语句级别的触发器函数应该总是返回 NULL。如果必要,触发器函数可以给调用它的执行者返回表中的数据行(一个类型为 HeapTuple 的数值)。那些在操作之前触发的行级触发器有以下选择:<br />
<br />
*它可以返回 NULL 以忽略对当前行的操作。这就指示执行器不要执行调用该触发器的行级别操作(对特定行的插入、修改或者删除)。<br />
<br />
*仅对行级别 INSERT 和 UPDATE 触发器而言,其返回将要被插入和更新的行。这样就允许触发器函数修改被插入或者更新的行。<br />
<br />
行级别 BEFORE 触发器并没有这些行为,它返回传进来行(也就是说,对 INSERT 和 UPDATE 触发器来说返回新行,对 DELETE 触发器来说返回被删除的行)。<br />
<br />
行级别 INSTEAD OF 触发器要么返回 NULL 以表示其没修改某视图的底层表,要么返回传进来的视图行(INSERT 和 UPDATE操作的 NEW 行, DELETE操作的 OLD 行)。一个非空的返回值标志着触发器修改了视图的相应数据。这将导致该命令所影响数据行的计数增加。对于 INSERT 和 UPDATE 操作,触发器在返回前可能修改 NEW 行。这将改变被 INSERT RETURNING 或 UPDATE RETURNING 返回的数据,当视图没能展示其数据时非常有用。<br />
<br />
对于在操作之后触发的行级别的触发器,其返回值会被忽略,因此他们可以返回NULL。<br />
<br />
如果多于一个触发器为同样的事件定义在同样的关系上, 触发器将按照由名字的字母顺序排序的顺序触发。 如果是事件之前触发的触发器,每个触发器返回的可能已经被修改过的行成为下一个触发器的输入。 如果任何事件之前触发的触发器返回 NULL 指针, 那么对该行的操作将被丢弃并且随后的触发器不会被触发。<br />
<br />
触发器的定义同样可以指定布尔值的 WHEN 条件,它将用来决定触发器是否应该被触发。行级触发器的 WHEN 条件可以检查该行某列的旧值和新值。(语句级触发器也可以有 WHEN 条件,尽管这个功能没那么有用。)对于BEFORE触发器,WHEN条件在函数执行前判定,所以使用WHEN条件与在函数中判定没有区别。然而,对于AFTER触发器,WHEN条件在行被更新后判定,它决定该事件是否加入被触发队列。所以,如果AFTER触发器WHEN条件没有返回真值,也就没有必要再将该事件放到触发队列,也没必要在语句结束后重新取得行。如果该触发器只定义在少量的行,这个功能对于修改大量行时就行有意义,将大大提升修改速度。INSTEAD OF触发器不支持WHEN条件。<br />
<br />
通常,行级 before 触发器用于检查或修改将要插入或者更新的数据。 比如,一个 before 触发器可以用于把当前时间插入一个 timestamp 字段, 或者跟踪该行的两个元素是否一致。行的 after 触发器多数用于填充或者更新其它表, 或者对其它表进行一致性检查。这么分工的原因是, after 触发器肯定可以看到该行的最后数值, 而 before 触发器不能;还可能有其它的 before 触发器在其后触发。 如果你没有具体的原因定义触发器是 before 还是 after,那么 before 触发器的效率高些, 因为操作相关的信息不必保存到语句的结尾。<br />
<br />
如果一个触发器函数执行 SQL 命令,然后这些命令可能再次触发触发器。 这就是所谓的级联触发器。对级联触发器的级联深度没有明确的限制。 有可能出现级联触发器导致同一个触发器的递归调用的情况; 比如,一个 INSERT 触发器可能执行一个命令, 把一个额外的行插入同一个表中,导致 INSERT 触发器再次激发。 避免这样的无穷递归的问题是触发器程序员的责任。<br />
<br />
在定义一个触发器的时候,我们可以声明一些参数。在触发器定义里面包含参数的目的是允许类似需求的不同触发器调用同一个函数。 比如,我们可能有一个通用的触发器函数,接受两个字段名字,把当前用户放在第一个,而当前时间戳在第二个。 只要我们写得恰当,那么这个触发器函数就可以和触发它的特定表无关。这样同一个函数就可以用于有着合适字段的任何表的 INSERT 事件,实现自动跟踪交易表中的记录创建之类的问题。如果定义成一个 UPDATE 触发器,我们还可以用它跟踪最后更新的事件。<br />
<br />
每种支持触发器的编程语言都有自己的方法让触发器函数得到输入数据。这些输入数据包括触发器事件的类型(比如,INSERT 或者 UPDATE)以及所有在 CREATE TRIGGER 里面列出的参数。对于行级触发器,输入数据也包括 INSERT 和 UPDATE 触发器的 NEW 行,和/或 UPDATE 和 DELETE 触发器的 OLD 行。语句级别的触发器目前没有任何方法检查改语句所修改行。<br />
<br />
== 数据更新的可见性 ==<br />
== 用C语言写触发器 ==<br />
== 一个完整的触发器示例 ==<br />
<br />
<br />
<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AD%E7%AB%A0&diff=208059.1第三十六章2013-09-20T03:57:56Z<p>Horen: </p>
<hr />
<div><br />
'''第36章 触发器'''<br />
----<br />
<br />
这章概括了关于触发器编写的内容。触发器可以用大多数过程语言编写,包括PL/pgSQL (第39章), PL/Tcl (第40章), PL/Perl (第41章), 还有 PL/Python (第42章)。<br />
阅读完本章以后,你可以参照你最喜欢的语言相关的章节,找出用该语言编写触发器的相关细节。<br />
尽管大多数人感觉用过程语言编写触发器相对容易些,但是用C语言同样也可以写出触发器来。<br />
目前还是不能用单纯的SQL语句编写触发器函数。<br />
<br />
== 触发器功能概述 ==<br />
== 数据更新的可见性 ==<br />
== 用C语言写触发器 ==<br />
== 一个完整的触发器示例 ==<br />
<br />
<br />
<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%B8%89%E5%8D%81%E5%85%AD%E7%AB%A0&diff=208049.1第三十六章2013-09-20T03:39:53Z<p>Horen: </p>
<hr />
<div><br />
'''第36章 触发器'''<br />
----<br />
<br />
== 触发器功能概述 ==<br />
== 数据更新的可见性 ==<br />
== 用C语言写触发器 ==<br />
== 一个完整的触发器示例 ==<br />
<br />
这章概括了关于触发器编写的内容。触发器可以用大多数过程语言编写,包括PL/pgSQL (Chapter 39), PL/Tcl (Chapter 40), PL/Perl (Chapter 41), 还有 PL/Python (Chapter 42)<br />
<br />
阅读完本章以后,你可以参照你最喜欢的语言相关的章节,找出用该语言编写触发器的相关细节。<br />
尽管大多数人感觉用过程语言编写触发器相对容易些,但是用C语言同样也可以写出触发器来。<br />
目前还是不能用单纯的SQL语句编写触发器函数。<br />
<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E6%96%87%E6%A1%A3%E7%BF%BB%E8%AF%91%E9%A1%B9%E7%9B%AE&diff=203569.1文档翻译项目2013-07-06T14:43:15Z<p>Horen: </p>
<hr />
<div>参考文档<br />
*[http://www.pgsqldb.org/mwiki/index.php/PostgreSQL_8.3_%E6%96%87%E6%A1%A3 8.3 wiki版本]<br />
*[http://www.pgsqldb.org/pgsqldoc-8.1c/index.html 8.1中文文档]<br />
*[http://wiki.postgresql.org/images/7/7b/PostgreSQL8.2.3doc.zip 8.2.3中文文档]<br />
*[http://developer.postgresql.org/pgdocs/postgres/index.html 9.1文档]<br />
*[http://wiki.postgresql.org/wiki/Help:Formatting wiki语法参考]<br />
<br />
校对的时候,使用<del> </del>标记原来的内容,然后写上修改后的东西。<br />
{|border="1"<br />
|其他语言模块<br />
|模块名字<br />
|标注者<br />
|-<br />
|python<br />
|[[psycopy]]<br />
|冇得弹<br />
|-<br />
|.net<br />
|[[npgsql]]<br />
|冇得弹<br />
|-<br />
|php<br />
|直接支持<br />
|冇得弹<br />
|}<br />
翻译任务<br />
{| border=1<br />
| 部分 <br />
| 章节<br />
| 标题<br />
| 翻译志愿者<br />
| 状态<br />
| 开始时间<br />
| 计划完成时间<br />
| 备注<br />
| 文件<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 1<br />
| Getting Started<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
| [[9.1第一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 2<br />
| The SQL Language<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第二章]]<br />
|-<br />
| Tutorial<br />
| 3<br />
| Advanced Features<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第三章]]<br />
|-<br />
| The SQL Language<br />
| 4<br />
| SQL Syntax<br />
| 冇得弹<br />
| 89%<br />
|<br />
|<br />
|<br />
|[[9.1第四章]]<br />
|-<br />
| The SQL Language<br />
| 5<br />
| Data Definition<br />
| 冇得弹<br />
| 98%<br />
|<br />
|<br />
|<br />
|[[9.1第五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| The SQL Language<br />
| 6<br />
| Data Manipulation<br />
| 冇得弹<br />
| '''完美'''<br />
| <br />
| <br />
| 这章的内容和8.1文档一样<br />
|[[9.1第六章]]<br />
|-<br />
| The SQL Language<br />
| 7<br />
| Queries<br />
| 冇得弹<br />
| <br />
| <br />
| <br />
| <br />
|[[9.1第七章]]<br />
|-<br />
| The SQL Language<br />
| 8<br />
| Data Types<br />
| 冇得弹<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第八章]]<br />
|-<br />
| The SQL Language<br />
| 9<br />
| Functions and Operators<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第九章]]<br />
|-<br />
| The SQL Language<br />
| 10<br />
| Type Conversion<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十章]]<br />
|-<br />
| The SQL Language<br />
| 11<br />
| Indexes<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十一章]]<br />
|-<br />
| The SQL Language<br />
| 12<br />
| Full Text Search<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十二章]]<br />
|-<br />
| The SQL Language<br />
| 13<br />
| Concurrency Control<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十三章]]<br />
|-<br />
| The SQL Language<br />
| 14<br />
| Performance Tips<br />
| 冇得弹<br />
|开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十四章]]<br />
|-<br />
| Server Administration<br />
| 15<br />
| Installation from Source Code<br />
| 紫轩<br />
| 基本完成<br />
|<br />
|<br />
|<br />
|[[9.1第十五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 16<br />
| Installation from Source Code on Windows<br />
| 紫轩<br />
| 完成 校正中 (iihero补校)<br />
|<br />
|<br />
| 有些计算机专业用语不懂,恐怕会让人误解<br />
|[[9.1第十六章]]<br />
|-<br />
| Server Administration<br />
| 17<br />
| Server Setup and Operation<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十七章]]<br />
|-<br />
| Server Administration<br />
| 18<br />
| Server Configuration<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十八章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 19<br />
| Client Authentication<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第十九章]]<br />
|-<br />
| Server Administration<br />
| 20<br />
| Database Roles<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十章]]<br />
|-<br />
| Server Administration<br />
| 21<br />
| Managing Databases<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 22<br />
| Localization<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十二章]]<br />
|-<br />
| Server Administration<br />
| 23<br />
| Routine Database Maintenance Tasks<br />
| 小燕子 <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十三章]]<br />
|-<br />
| Server Administration<br />
| 24<br />
| Backup and Restore<br />
| 星海游侠<br />
| 完成了一半<br />
|<br />
|<br />
|<br />
|[[9.1第二十四章]]<br />
|-<br />
| Server Administration<br />
| 25<br />
| High Availability, Load Balancing, and Replication<br />
| 星海游侠<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十五章]]<br />
|-<br />
| Server Administration<br />
| 26<br />
| Recovery Configuration<br />
| 星海游侠<br />
| 完成,待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十六章]]<br />
|-<br />
| Server Administration<br />
| 27<br />
| Monitoring Database Activity<br />
| 泥鳅<br />
| 完成,谁来校验下<br />
|<br />
|<br />
|<br />
|[[9.1第二十七章]]<br />
|-<br />
| Server Administration<br />
| 28<br />
| Monitoring Disk Usage<br />
| 泥鳅<br />
| 完成,很简单,不需要校验<br />
|<br />
|<br />
|<br />
|[[9.1第二十八章]]<br />
|-<br />
| Server Administration<br />
| 29<br />
| Reliability and the Write-Ahead Log<br />
| 小燕子<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十九章]]<br />
|-<br />
| Server Administration<br />
| 30<br />
| Regression Tests<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十章]]<br />
|-style="font-style:italic;color:green;"<br />
| Client Interfaces<br />
| 31<br />
| ibpq - C Library<br />
| 泥鳅<br />
| Working in progress<br />
| 2011-7-17<br />
| 工作量极大,不好估算时间<br />
|<br />
|[[9.1第三十一章]]<br />
|-<br />
| Client Interfaces<br />
| 32<br />
| Large Objects<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十二章]]<br />
|-<br />
| Client Interfaces<br />
| 33<br />
| ECPG - Embedded SQL in C<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十三章]]<br />
|-<br />
| Client Interfaces<br />
| 34<br />
| The Information Schema<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十四章]]<br />
|-style="font-style:italic;color:red;"<br />
| Server Programming<br />
| 35<br />
| Extending SQL<br />
| iihero<br />
| '''Start working'''<br />
|<br />
|<br />
|<br />
|[[9.1第三十五章]]<br />
|-<br />
| Server Programming<br />
| 36<br />
| Triggers<br />
| 任洪彩<br />
| 开始了<br />
| 2013-07-06<br />
|<br />
|<br />
|[[9.1第三十六章]]<br />
|-<br />
| Server Programming<br />
| 37<br />
| The Rule System<br />
| 小燕子<br />
| i'm here<br />
|<br />
|<br />
|<br />
|[[9.1第三十七章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Programming<br />
| 38<br />
| Procedural Languages<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第三十八章]]<br />
|-<br />
| Server Programming<br />
| 39<br />
| PL/pgSQL - SQL Procedural Language<br />
| 杨拼<br />
|开始了。。。<br />
|<br />
|<br />
|<br />
|[[9.1第三十九章]]<br />
|-<br />
| Server Programming<br />
| 40<br />
| PL/Tcl - Tcl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十章]]<br />
|-<br />
| Server Programming<br />
| 41<br />
| PL/Perl - Perl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十一章]]<br />
|-<br />
| Server Programming<br />
| 42<br />
| PL/Python - Python Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十二章]]<br />
|-<br />
| Server Programming<br />
| 43<br />
| Server Programming Interface<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十三章]]<br />
|-<br />
| Reference<br />
| I<br />
| SQL Commands<br />
| gvim<br />
| 开始 23/150<br />
|<br />
|<br />
|<br />
|[[9.1参考1]]<br />
|-<br />
| Reference<br />
| II<br />
| PostgreSQL Client Applications<br />
| eastday<br />
| 刚刚开始。<br />
|<br />
|<br />
|大多内容和8.1相同.<br />
|[[9.1参考2]]<br />
|-<br />
| Reference<br />
| III<br />
| PostgreSQL Server Applications<br />
| 任洪彩<br />
| 马上开始<br />
|<br />
|<br />
|<br />
|[[9.1参考3]]<br />
|- style="font-style:italic;color:red;"<br />
| Internals<br />
| 44<br />
| Overview of PostgreSQL Internals<br />
| Galy<br />
| 着手<br />
|<br />
|<br />
|<br />
| [[9.1第44章]]<br />
|-<br />
| Internals<br />
| 45<br />
| System Catalogs<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第45章]]<br />
|-<br />
| Internals<br />
| 46<br />
| Frontend/Backend Protocol<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第46章]]<br />
|-<br />
| Internals<br />
| 47<br />
| PostgreSQL Coding Conventions<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第47章]]<br />
|-<br />
| Internals<br />
| 48<br />
| Native Language Support<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第48章]]<br />
|-<br />
| Internals<br />
| 49<br />
| Writing A Procedural Language Handler<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第49章]]<br />
|-<br />
| Internals<br />
| 50<br />
| Genetic Query Optimizer<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第50章]]<br />
|-<br />
| Internals<br />
| 51<br />
| Index Access Method Interface Definition<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第51章]]<br />
|-<br />
| Internals<br />
| 52<br />
| GiST Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第52章]]<br />
|-<br />
| Internals<br />
| 53<br />
| GIN Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第53章]]<br />
|-<br />
| Internals<br />
| 54<br />
| Database Physical Storage<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第54章]]<br />
|-<br />
| Internals<br />
| 55<br />
| BKI Backend Interface<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第55章]]<br />
|-<br />
| Internals<br />
| 56<br />
| How the Planner Uses Statistics<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第56章]]<br />
|-<br />
|}<br />
<br />
志愿者简介<br />
<br />
感谢为我们的文档付出了的无私的奉献。<br />
{|border=1<br />
|网名来胳<br />
|猫<br />
|-<br />
|''泥鳅''<br />
|<br />
|-<br />
|''小神厨'' <br />
|anliulin1986 _at_ sina.com<br />
|-<br />
|''Galy''<br />
|galylee _at_ gmail.com<br />
|-<br />
|''hxre''<br />
|hxre _at_ hotmail.com<br />
|-<br />
|''冇得弹''<br />
|zelda_ok _at_ 163.com<br />
|-<br />
|星海游侠<br />
|<br />
|-<br />
|''iihero''<br />
|iihero _at_ qq.com<br />
|}<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E6%96%87%E6%A1%A3%E7%BF%BB%E8%AF%91%E9%A1%B9%E7%9B%AE&diff=203559.1文档翻译项目2013-07-06T13:03:17Z<p>Horen: </p>
<hr />
<div>参考文档<br />
*[http://www.pgsqldb.org/mwiki/index.php/PostgreSQL_8.3_%E6%96%87%E6%A1%A3 8.3 wiki版本]<br />
*[http://www.pgsqldb.org/pgsqldoc-8.1c/index.html 8.1中文文档]<br />
*[http://wiki.postgresql.org/images/7/7b/PostgreSQL8.2.3doc.zip 8.2.3中文文档]<br />
*[http://developer.postgresql.org/pgdocs/postgres/index.html 9.1文档]<br />
*[http://wiki.postgresql.org/wiki/Help:Formatting wiki语法参考]<br />
<br />
校对的时候,使用<del> </del>标记原来的内容,然后写上修改后的东西。<br />
{|border="1"<br />
|其他语言模块<br />
|模块名字<br />
|标注者<br />
|-<br />
|python<br />
|[[psycopy]]<br />
|冇得弹<br />
|-<br />
|.net<br />
|[[npgsql]]<br />
|冇得弹<br />
|-<br />
|php<br />
|直接支持<br />
|冇得弹<br />
|}<br />
翻译任务<br />
{| border=1<br />
| 部分 <br />
| 章节<br />
| 标题<br />
| 翻译志愿者<br />
| 状态<br />
| 开始时间<br />
| 计划完成时间<br />
| 备注<br />
| 文件<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 1<br />
| Getting Started<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
| [[9.1第一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 2<br />
| The SQL Language<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第二章]]<br />
|-<br />
| Tutorial<br />
| 3<br />
| Advanced Features<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第三章]]<br />
|-<br />
| The SQL Language<br />
| 4<br />
| SQL Syntax<br />
| 冇得弹<br />
| 89%<br />
|<br />
|<br />
|<br />
|[[9.1第四章]]<br />
|-<br />
| The SQL Language<br />
| 5<br />
| Data Definition<br />
| 冇得弹<br />
| 98%<br />
|<br />
|<br />
|<br />
|[[9.1第五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| The SQL Language<br />
| 6<br />
| Data Manipulation<br />
| 冇得弹<br />
| '''完美'''<br />
| <br />
| <br />
| 这章的内容和8.1文档一样<br />
|[[9.1第六章]]<br />
|-<br />
| The SQL Language<br />
| 7<br />
| Queries<br />
| 冇得弹<br />
| <br />
| <br />
| <br />
| <br />
|[[9.1第七章]]<br />
|-<br />
| The SQL Language<br />
| 8<br />
| Data Types<br />
| 冇得弹<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第八章]]<br />
|-<br />
| The SQL Language<br />
| 9<br />
| Functions and Operators<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第九章]]<br />
|-<br />
| The SQL Language<br />
| 10<br />
| Type Conversion<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十章]]<br />
|-<br />
| The SQL Language<br />
| 11<br />
| Indexes<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十一章]]<br />
|-<br />
| The SQL Language<br />
| 12<br />
| Full Text Search<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十二章]]<br />
|-<br />
| The SQL Language<br />
| 13<br />
| Concurrency Control<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十三章]]<br />
|-<br />
| The SQL Language<br />
| 14<br />
| Performance Tips<br />
| 冇得弹<br />
|开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十四章]]<br />
|-<br />
| Server Administration<br />
| 15<br />
| Installation from Source Code<br />
| 紫轩<br />
| 基本完成<br />
|<br />
|<br />
|<br />
|[[9.1第十五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 16<br />
| Installation from Source Code on Windows<br />
| 紫轩<br />
| 完成 校正中 (iihero补校)<br />
|<br />
|<br />
| 有些计算机专业用语不懂,恐怕会让人误解<br />
|[[9.1第十六章]]<br />
|-<br />
| Server Administration<br />
| 17<br />
| Server Setup and Operation<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十七章]]<br />
|-<br />
| Server Administration<br />
| 18<br />
| Server Configuration<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十八章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 19<br />
| Client Authentication<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第十九章]]<br />
|-<br />
| Server Administration<br />
| 20<br />
| Database Roles<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十章]]<br />
|-<br />
| Server Administration<br />
| 21<br />
| Managing Databases<br />
| 任洪彩<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 22<br />
| Localization<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十二章]]<br />
|-<br />
| Server Administration<br />
| 23<br />
| Routine Database Maintenance Tasks<br />
| 小燕子 <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十三章]]<br />
|-<br />
| Server Administration<br />
| 24<br />
| Backup and Restore<br />
| 星海游侠<br />
| 完成了一半<br />
|<br />
|<br />
|<br />
|[[9.1第二十四章]]<br />
|-<br />
| Server Administration<br />
| 25<br />
| High Availability, Load Balancing, and Replication<br />
| 星海游侠<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十五章]]<br />
|-<br />
| Server Administration<br />
| 26<br />
| Recovery Configuration<br />
| 星海游侠<br />
| 完成,待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十六章]]<br />
|-<br />
| Server Administration<br />
| 27<br />
| Monitoring Database Activity<br />
| 泥鳅<br />
| 完成,谁来校验下<br />
|<br />
|<br />
|<br />
|[[9.1第二十七章]]<br />
|-<br />
| Server Administration<br />
| 28<br />
| Monitoring Disk Usage<br />
| 泥鳅<br />
| 完成,很简单,不需要校验<br />
|<br />
|<br />
|<br />
|[[9.1第二十八章]]<br />
|-<br />
| Server Administration<br />
| 29<br />
| Reliability and the Write-Ahead Log<br />
| 小燕子<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十九章]]<br />
|-<br />
| Server Administration<br />
| 30<br />
| Regression Tests<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十章]]<br />
|-style="font-style:italic;color:green;"<br />
| Client Interfaces<br />
| 31<br />
| ibpq - C Library<br />
| 泥鳅<br />
| Working in progress<br />
| 2011-7-17<br />
| 工作量极大,不好估算时间<br />
|<br />
|[[9.1第三十一章]]<br />
|-<br />
| Client Interfaces<br />
| 32<br />
| Large Objects<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十二章]]<br />
|-<br />
| Client Interfaces<br />
| 33<br />
| ECPG - Embedded SQL in C<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十三章]]<br />
|-<br />
| Client Interfaces<br />
| 34<br />
| The Information Schema<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十四章]]<br />
|-style="font-style:italic;color:red;"<br />
| Server Programming<br />
| 35<br />
| Extending SQL<br />
| iihero<br />
| '''Start working'''<br />
|<br />
|<br />
|<br />
|[[9.1第三十五章]]<br />
|-<br />
| Server Programming<br />
| 36<br />
| Triggers<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十六章]]<br />
|-<br />
| Server Programming<br />
| 37<br />
| The Rule System<br />
| 小燕子<br />
| i'm here<br />
|<br />
|<br />
|<br />
|[[9.1第三十七章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Programming<br />
| 38<br />
| Procedural Languages<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第三十八章]]<br />
|-<br />
| Server Programming<br />
| 39<br />
| PL/pgSQL - SQL Procedural Language<br />
| 杨拼<br />
|开始了。。。<br />
|<br />
|<br />
|<br />
|[[9.1第三十九章]]<br />
|-<br />
| Server Programming<br />
| 40<br />
| PL/Tcl - Tcl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十章]]<br />
|-<br />
| Server Programming<br />
| 41<br />
| PL/Perl - Perl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十一章]]<br />
|-<br />
| Server Programming<br />
| 42<br />
| PL/Python - Python Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十二章]]<br />
|-<br />
| Server Programming<br />
| 43<br />
| Server Programming Interface<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十三章]]<br />
|-<br />
| Reference<br />
| I<br />
| SQL Commands<br />
| gvim<br />
| 开始 23/150<br />
|<br />
|<br />
|<br />
|[[9.1参考1]]<br />
|-<br />
| Reference<br />
| II<br />
| PostgreSQL Client Applications<br />
| eastday<br />
| 刚刚开始。<br />
|<br />
|<br />
|大多内容和8.1相同.<br />
|[[9.1参考2]]<br />
|-<br />
| Reference<br />
| III<br />
| PostgreSQL Server Applications<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1参考3]]<br />
|- style="font-style:italic;color:red;"<br />
| Internals<br />
| 44<br />
| Overview of PostgreSQL Internals<br />
| Galy<br />
| 着手<br />
|<br />
|<br />
|<br />
| [[9.1第44章]]<br />
|-<br />
| Internals<br />
| 45<br />
| System Catalogs<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第45章]]<br />
|-<br />
| Internals<br />
| 46<br />
| Frontend/Backend Protocol<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第46章]]<br />
|-<br />
| Internals<br />
| 47<br />
| PostgreSQL Coding Conventions<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第47章]]<br />
|-<br />
| Internals<br />
| 48<br />
| Native Language Support<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第48章]]<br />
|-<br />
| Internals<br />
| 49<br />
| Writing A Procedural Language Handler<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第49章]]<br />
|-<br />
| Internals<br />
| 50<br />
| Genetic Query Optimizer<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第50章]]<br />
|-<br />
| Internals<br />
| 51<br />
| Index Access Method Interface Definition<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第51章]]<br />
|-<br />
| Internals<br />
| 52<br />
| GiST Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第52章]]<br />
|-<br />
| Internals<br />
| 53<br />
| GIN Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第53章]]<br />
|-<br />
| Internals<br />
| 54<br />
| Database Physical Storage<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第54章]]<br />
|-<br />
| Internals<br />
| 55<br />
| BKI Backend Interface<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第55章]]<br />
|-<br />
| Internals<br />
| 56<br />
| How the Planner Uses Statistics<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第56章]]<br />
|-<br />
|}<br />
<br />
志愿者简介<br />
<br />
感谢为我们的文档付出了的无私的奉献。<br />
{|border=1<br />
|网名来胳<br />
|猫<br />
|-<br />
|''泥鳅''<br />
|<br />
|-<br />
|''小神厨'' <br />
|anliulin1986 _at_ sina.com<br />
|-<br />
|''Galy''<br />
|galylee _at_ gmail.com<br />
|-<br />
|''hxre''<br />
|hxre _at_ hotmail.com<br />
|-<br />
|''冇得弹''<br />
|zelda_ok _at_ 163.com<br />
|-<br />
|星海游侠<br />
|<br />
|-<br />
|''iihero''<br />
|iihero _at_ qq.com<br />
|}<br />
<br />
[[Category:Chinese]]</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203549.1第二十一章2013-07-06T13:01:13Z<p>Horen: /* 表空间 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的。缺省情况下,它拷贝名字叫 template1 的标准系统数据库。 所以该数据库是创建新数据库的"模板"。如果你给 template1 增加对象,这些对象将被拷贝到随后创建的用户数据库中。这样的行为允许节点对数据库中的标准套件进行修改。 比如,如果你把过程语言 PL/pgSQL 安装到 template1 里,那么你在创建用户数据库的时候它们就会自动可得,而不需要额外的动作。<br />
<br />
还有第二个标准的系统数据库,叫 template0。 这个数据库包含和 template1 一开始时一样的数据内容,也就是说,只有当前版本的 PostgreSQL 标准对象。在数据库集群初始化之后,不应该对 template0 做任何修改。通过告诉 CREATE DATABASE 使用 template0 而不是 template1 进行拷贝,你可以创建一个"纯净"的用户数据库,它不会包含任何 template1 里节点所附加的东西。这一点在恢复 pg_dump 转储的时候是非常方便的:转储脚本应该在一个纯洁的数据库中恢复以确保我们创建了被转储出的数据库中的正确内容, 而不和可能已经存在在 template1 中的对象相冲突。<br />
<br />
另一个不用 template1 而用 template0 去拷贝的原因是,拷贝 template0 时你可以指定新的编码和本地化设置,而拷贝 template1 时必须使用相同的设置。因为 template1 可能已经指定了编码和本地化设置,而 template0 却没有。<br />
<br />
要通过拷贝 template0 的方式创建一个数据库,使用命令:<br />
<br />
<pre>CREATE DATABASE dbname TEMPLATE template0;</pre><br />
<br />
这条命令是在 SQL 环境里的,另一个命令:<br />
<br />
<pre>createdb -T template0 dbname</pre><br />
<br />
是从shell里执行的。<br />
<br />
我们可以创建额外的模板数据库,而且实际上只要为 CREATE DATABASE 指定模版名字就可以拷贝集群中的任何一个数据库。不过,我们必需明白,这个功能并非一般性的"COPY DATABASE"工具。这里有个限制是源数据库在拷贝时不能连接任何会话。如果源数据库存在任何其他的连接,在使用 CREATE DATABASE 拷贝时就会失败,在拷贝进行中,试图连接到源数据库的操作都会被阻止。<br />
<br />
在 pg_database 里针对每个数据库有两个有用的标志:字段 datistemplate 和 datallowconn。datistemplate 表示该数据库是否准备用做 CREATE DATABASE 的模板。如果设置了这个标志,那么该数据库可以由任何有 CREATEDB 权限的用户克隆;如果没有设置,那么只有超级用户和该数据库的所有者可以克隆它。如果 datallowconn 为假,那么将不允许与该数据库发生任何新的连接(不过现有的会话不会因为把该标志设置为假而被中断)。template0 数据库通常被标记为 datallowconn = false 以避免对它的修改。template0 和 template1应该总是标记为datistemplate = true。<br />
<br />
<blockquote><br />
注意:除了 template1 在CREATE DATABASE 时是缺省的源数据库外,template1 和 template0 没有任何其他特殊状态。例如,你可以删除template1并根据template0重新创建以消除任何不利的影响。当你不小心污染了template1时推荐这种做法。(删除template1前,必须先在pg_database中设置其datistemplate属性为false。)数据库集群在初始化时还会自动创建postgres数据库。其做为用户和应用程序缺省连接的数据库。它只是template1的一个简单拷贝,必要时也可以删除和重建。<br />
</blockquote><br />
<br />
==数据库配置==<br />
<br />
回顾一下 Chapter 18,我们知道PostgreSQL 服务器提供了大量的运行时配置变量。你可以为许多这样的变量设置数据库相关的缺省值。<br />
<br />
比如,如果由于某种原因,你想关闭某个数据库上的 GEQO 优化器,通常情况下你会在所有数据库中关闭它,或者是保证每个连接过来的客户端都很小心地设置了 SET geqo TO off。要令这个设置在特定数据库里成为缺省,你可以执行下面的命令:<br />
<br />
<pre>ALTER DATABASE mydb SET geqo TO off;</pre><br />
<br />
这样就保存了设置(但不是立即生效)。在随后的连接中,看起来好像是在会话开始后马上调用了 SET geqo TO off;请注意用户仍然可以在该会话中更改这个设置;它只是缺省。要撤消某个设置,使用ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
删除数据库的命令是 DROP DATABASE:<br />
<br />
<pre>DROP DATABASE name;</pre><br />
<br />
只有数据库的所有者,或者超级用户可以删除数据库。删除数据库会删除数据库中包括的所有对象。数据库的删除是不可恢复的。<br />
<br />
你不能使用 DROP DATABASE 删除与你连接的数据库。不过,你可以联接到其他数据库去执行,包括template1数据库,template1也是你删除集群中最后一个用户数据库的唯一方法。<br />
<br />
方便起见,有一个shell程序也可以删除数据库,dropdb:<br />
<br />
<pre>dropdb dbname</pre><br />
<br />
(与createdb不同,它缺省不是删除与当前用户同名的数据库。)<br />
<br />
==表空间==<br />
<br />
PostgreSQL 里的表空间允许数据库管理员在文件系统里定义那些存储数据库对象的文件存放的位置。一旦创建了表空间,那么就可以在创建数据库对象的时候引用它。<br />
<br />
通过使用表空间,管理员可以控制一个 PostgreSQL 安装的磁盘布局。这么做至少有两个好处。首先,如果初始化集群所在的分区或者卷用光了空间,而又不能扩展空间,那么可以在另一个分区上创建和使用表空间,直到系统可以重新配置。<br />
<br />
第二,表空间允许管理员根据数据库对象的使用模式安排数据位置,从而优化性能。比如,一个很频繁使用的索引可以放在非常快的,并且非常可靠的磁盘上,比如一种非常贵的固态设备。而同时,一个存储归档的数据,很少使用的或者对性能要求不高的表可以存储在一个没那么昂贵,比较慢的磁盘系统上。<br />
<br />
要定义一个表空间,使用命令 CREATE TABLESPACE,例如:<br />
<br />
<pre>CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';</pre><br />
<br />
这个位置必须是一个现有的空目录,并且属于 PostgreSQL 系统用户。所有随后在该表空间创建的对象都将被存放在这个目录下的文件里。<br />
<br />
<blockquote><br />
注意: 通常在一个逻辑文件系统上建立多个表空间没有什么意义,因为你无法控制一个逻辑文件系统里的不同文件的位置。 不过,PostgreSQL 并不做这方面的任何强制,并且它实际上并不知道你的系统上的文件系统边界。它只是在你指定的目录里存储文件。</blockquote><br />
<br />
创建表空间必须用数据库超级用户身份进行,但是在那之后,你就可以允许普通数据库用户利用它了。你只需在表空间上给这些用户授予 CREATE 权限。<br />
<br />
表、索引和整个数据库都可以放在特定的表空间里。想要这么做的话,在给定表空间上有 CREATE 权限的用户必须把表空间的名字以一个参数的形式传递给相关的命令。比如,下面的命令在表空间 space1 上创建一个表:<br />
<br />
<pre>CREATE TABLE foo(i int) TABLESPACE space1;</pre><br />
<br />
另外,还可以设置缺省的表空间:<br />
<br />
<pre>SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);</pre><br />
<br />
只要 default_tablespace 设置为某个非空字符串,那么它就为没有明确使用 TABLESPACE 子句的 CREATE TABLE 和 CREATE INDEX 命令提供一个隐含的 TABLESPACE 子句。<br />
<br />
还有一个临时表空间 temp_tablespaces,它用来指定临时表、索引以及临时文件的存放位置,比如用于大数据排序。此时可以指定多个表空间,而不是一个,这样临时对象可以被装载到多个表空间里。但是每个临时对象只被创建在一个随机的表空间里。<br />
<br />
与一个数据库相关联的表空间用于存储该数据库的系统表,而且,如果在创建表、索引和临时文件时没有指定TABLESPACE且没有设置default_tablespace以及temp_tablespace,那么也会缺省使用该空间。如果创建数据库时没有给它声明一个表空间,那么它使用与它拷贝的模版数据库相同的表空间。<br />
<br />
数据库集群初始化时自动创建两个表空间。pg_global 表空间用于共享的系统表。pg_default 是 template1 和 template0 数据库的缺省表空间(因此,这个表空间也将是任何其它数据库的缺省表空间,除非在 CREATE DATABASE 的时候使用了明确的 TABLESPACE 子句)。<br />
<br />
表空间一但被创建就可以被集群中任何数据库使用,前提时使用者需要拥有相应的权限。这也意味着只要有任意一个数据库还在使用,该表空间就不能被删除。<br />
<br />
删除一个空的表空间,使用命令 DROP TABLESPACE:<br />
<br />
要查询已存在的表空间,可以查询系统表pg_tablespace,例如<br />
<br />
<pre>SELECT spcname FROM pg_tablespace;</pre><br />
<br />
还可以使用psql程序提供的 \db 元命令来列出已存在的表空间。<br />
<br />
PostgreSQL 使用符号链接来简单表空间的实现。这也意味着表空间只可用在支持符号连接的系统上。<br />
<br />
目录 $PGDATA/pg_tblspc 中存放了集群中的符号连接来指向每个非内置的表空间。尽管不推荐,可以手动重定义这些链接来调整表空间布局。两个警告:不在要服务器运行时做;服务器重启后记得更新新位置到pg_tablespace 表。(否则,pg_dump仍然会输出先前位置的表空间。)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203539.1第二十一章2013-07-06T12:57:32Z<p>Horen: /* 表空间 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的。缺省情况下,它拷贝名字叫 template1 的标准系统数据库。 所以该数据库是创建新数据库的"模板"。如果你给 template1 增加对象,这些对象将被拷贝到随后创建的用户数据库中。这样的行为允许节点对数据库中的标准套件进行修改。 比如,如果你把过程语言 PL/pgSQL 安装到 template1 里,那么你在创建用户数据库的时候它们就会自动可得,而不需要额外的动作。<br />
<br />
还有第二个标准的系统数据库,叫 template0。 这个数据库包含和 template1 一开始时一样的数据内容,也就是说,只有当前版本的 PostgreSQL 标准对象。在数据库集群初始化之后,不应该对 template0 做任何修改。通过告诉 CREATE DATABASE 使用 template0 而不是 template1 进行拷贝,你可以创建一个"纯净"的用户数据库,它不会包含任何 template1 里节点所附加的东西。这一点在恢复 pg_dump 转储的时候是非常方便的:转储脚本应该在一个纯洁的数据库中恢复以确保我们创建了被转储出的数据库中的正确内容, 而不和可能已经存在在 template1 中的对象相冲突。<br />
<br />
另一个不用 template1 而用 template0 去拷贝的原因是,拷贝 template0 时你可以指定新的编码和本地化设置,而拷贝 template1 时必须使用相同的设置。因为 template1 可能已经指定了编码和本地化设置,而 template0 却没有。<br />
<br />
要通过拷贝 template0 的方式创建一个数据库,使用命令:<br />
<br />
<pre>CREATE DATABASE dbname TEMPLATE template0;</pre><br />
<br />
这条命令是在 SQL 环境里的,另一个命令:<br />
<br />
<pre>createdb -T template0 dbname</pre><br />
<br />
是从shell里执行的。<br />
<br />
我们可以创建额外的模板数据库,而且实际上只要为 CREATE DATABASE 指定模版名字就可以拷贝集群中的任何一个数据库。不过,我们必需明白,这个功能并非一般性的"COPY DATABASE"工具。这里有个限制是源数据库在拷贝时不能连接任何会话。如果源数据库存在任何其他的连接,在使用 CREATE DATABASE 拷贝时就会失败,在拷贝进行中,试图连接到源数据库的操作都会被阻止。<br />
<br />
在 pg_database 里针对每个数据库有两个有用的标志:字段 datistemplate 和 datallowconn。datistemplate 表示该数据库是否准备用做 CREATE DATABASE 的模板。如果设置了这个标志,那么该数据库可以由任何有 CREATEDB 权限的用户克隆;如果没有设置,那么只有超级用户和该数据库的所有者可以克隆它。如果 datallowconn 为假,那么将不允许与该数据库发生任何新的连接(不过现有的会话不会因为把该标志设置为假而被中断)。template0 数据库通常被标记为 datallowconn = false 以避免对它的修改。template0 和 template1应该总是标记为datistemplate = true。<br />
<br />
<blockquote><br />
注意:除了 template1 在CREATE DATABASE 时是缺省的源数据库外,template1 和 template0 没有任何其他特殊状态。例如,你可以删除template1并根据template0重新创建以消除任何不利的影响。当你不小心污染了template1时推荐这种做法。(删除template1前,必须先在pg_database中设置其datistemplate属性为false。)数据库集群在初始化时还会自动创建postgres数据库。其做为用户和应用程序缺省连接的数据库。它只是template1的一个简单拷贝,必要时也可以删除和重建。<br />
</blockquote><br />
<br />
==数据库配置==<br />
<br />
回顾一下 Chapter 18,我们知道PostgreSQL 服务器提供了大量的运行时配置变量。你可以为许多这样的变量设置数据库相关的缺省值。<br />
<br />
比如,如果由于某种原因,你想关闭某个数据库上的 GEQO 优化器,通常情况下你会在所有数据库中关闭它,或者是保证每个连接过来的客户端都很小心地设置了 SET geqo TO off。要令这个设置在特定数据库里成为缺省,你可以执行下面的命令:<br />
<br />
<pre>ALTER DATABASE mydb SET geqo TO off;</pre><br />
<br />
这样就保存了设置(但不是立即生效)。在随后的连接中,看起来好像是在会话开始后马上调用了 SET geqo TO off;请注意用户仍然可以在该会话中更改这个设置;它只是缺省。要撤消某个设置,使用ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
删除数据库的命令是 DROP DATABASE:<br />
<br />
<pre>DROP DATABASE name;</pre><br />
<br />
只有数据库的所有者,或者超级用户可以删除数据库。删除数据库会删除数据库中包括的所有对象。数据库的删除是不可恢复的。<br />
<br />
你不能使用 DROP DATABASE 删除与你连接的数据库。不过,你可以联接到其他数据库去执行,包括template1数据库,template1也是你删除集群中最后一个用户数据库的唯一方法。<br />
<br />
方便起见,有一个shell程序也可以删除数据库,dropdb:<br />
<br />
<pre>dropdb dbname</pre><br />
<br />
(与createdb不同,它缺省不是删除与当前用户同名的数据库。)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
<pre>CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';</pre><br />
<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
<blockquote><br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.</blockquote><br />
<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
<pre>CREATE TABLE foo(i int) TABLESPACE space1;</pre><br />
<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
<pre>SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);</pre><br />
<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
<pre>SELECT spcname FROM pg_tablespace;</pre><br />
<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203529.1第二十一章2013-07-06T12:55:15Z<p>Horen: /* 删除数据库 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的。缺省情况下,它拷贝名字叫 template1 的标准系统数据库。 所以该数据库是创建新数据库的"模板"。如果你给 template1 增加对象,这些对象将被拷贝到随后创建的用户数据库中。这样的行为允许节点对数据库中的标准套件进行修改。 比如,如果你把过程语言 PL/pgSQL 安装到 template1 里,那么你在创建用户数据库的时候它们就会自动可得,而不需要额外的动作。<br />
<br />
还有第二个标准的系统数据库,叫 template0。 这个数据库包含和 template1 一开始时一样的数据内容,也就是说,只有当前版本的 PostgreSQL 标准对象。在数据库集群初始化之后,不应该对 template0 做任何修改。通过告诉 CREATE DATABASE 使用 template0 而不是 template1 进行拷贝,你可以创建一个"纯净"的用户数据库,它不会包含任何 template1 里节点所附加的东西。这一点在恢复 pg_dump 转储的时候是非常方便的:转储脚本应该在一个纯洁的数据库中恢复以确保我们创建了被转储出的数据库中的正确内容, 而不和可能已经存在在 template1 中的对象相冲突。<br />
<br />
另一个不用 template1 而用 template0 去拷贝的原因是,拷贝 template0 时你可以指定新的编码和本地化设置,而拷贝 template1 时必须使用相同的设置。因为 template1 可能已经指定了编码和本地化设置,而 template0 却没有。<br />
<br />
要通过拷贝 template0 的方式创建一个数据库,使用命令:<br />
<br />
<pre>CREATE DATABASE dbname TEMPLATE template0;</pre><br />
<br />
这条命令是在 SQL 环境里的,另一个命令:<br />
<br />
<pre>createdb -T template0 dbname</pre><br />
<br />
是从shell里执行的。<br />
<br />
我们可以创建额外的模板数据库,而且实际上只要为 CREATE DATABASE 指定模版名字就可以拷贝集群中的任何一个数据库。不过,我们必需明白,这个功能并非一般性的"COPY DATABASE"工具。这里有个限制是源数据库在拷贝时不能连接任何会话。如果源数据库存在任何其他的连接,在使用 CREATE DATABASE 拷贝时就会失败,在拷贝进行中,试图连接到源数据库的操作都会被阻止。<br />
<br />
在 pg_database 里针对每个数据库有两个有用的标志:字段 datistemplate 和 datallowconn。datistemplate 表示该数据库是否准备用做 CREATE DATABASE 的模板。如果设置了这个标志,那么该数据库可以由任何有 CREATEDB 权限的用户克隆;如果没有设置,那么只有超级用户和该数据库的所有者可以克隆它。如果 datallowconn 为假,那么将不允许与该数据库发生任何新的连接(不过现有的会话不会因为把该标志设置为假而被中断)。template0 数据库通常被标记为 datallowconn = false 以避免对它的修改。template0 和 template1应该总是标记为datistemplate = true。<br />
<br />
<blockquote><br />
注意:除了 template1 在CREATE DATABASE 时是缺省的源数据库外,template1 和 template0 没有任何其他特殊状态。例如,你可以删除template1并根据template0重新创建以消除任何不利的影响。当你不小心污染了template1时推荐这种做法。(删除template1前,必须先在pg_database中设置其datistemplate属性为false。)数据库集群在初始化时还会自动创建postgres数据库。其做为用户和应用程序缺省连接的数据库。它只是template1的一个简单拷贝,必要时也可以删除和重建。<br />
</blockquote><br />
<br />
==数据库配置==<br />
<br />
回顾一下 Chapter 18,我们知道PostgreSQL 服务器提供了大量的运行时配置变量。你可以为许多这样的变量设置数据库相关的缺省值。<br />
<br />
比如,如果由于某种原因,你想关闭某个数据库上的 GEQO 优化器,通常情况下你会在所有数据库中关闭它,或者是保证每个连接过来的客户端都很小心地设置了 SET geqo TO off。要令这个设置在特定数据库里成为缺省,你可以执行下面的命令:<br />
<br />
<pre>ALTER DATABASE mydb SET geqo TO off;</pre><br />
<br />
这样就保存了设置(但不是立即生效)。在随后的连接中,看起来好像是在会话开始后马上调用了 SET geqo TO off;请注意用户仍然可以在该会话中更改这个设置;它只是缺省。要撤消某个设置,使用ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
删除数据库的命令是 DROP DATABASE:<br />
<br />
<pre>DROP DATABASE name;</pre><br />
<br />
只有数据库的所有者,或者超级用户可以删除数据库。删除数据库会删除数据库中包括的所有对象。数据库的删除是不可恢复的。<br />
<br />
你不能使用 DROP DATABASE 删除与你连接的数据库。不过,你可以联接到其他数据库去执行,包括template1数据库,template1也是你删除集群中最后一个用户数据库的唯一方法。<br />
<br />
方便起见,有一个shell程序也可以删除数据库,dropdb:<br />
<br />
<pre>dropdb dbname</pre><br />
<br />
(与createdb不同,它缺省不是删除与当前用户同名的数据库。)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203519.1第二十一章2013-07-06T12:51:24Z<p>Horen: /* 数据库配置 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的。缺省情况下,它拷贝名字叫 template1 的标准系统数据库。 所以该数据库是创建新数据库的"模板"。如果你给 template1 增加对象,这些对象将被拷贝到随后创建的用户数据库中。这样的行为允许节点对数据库中的标准套件进行修改。 比如,如果你把过程语言 PL/pgSQL 安装到 template1 里,那么你在创建用户数据库的时候它们就会自动可得,而不需要额外的动作。<br />
<br />
还有第二个标准的系统数据库,叫 template0。 这个数据库包含和 template1 一开始时一样的数据内容,也就是说,只有当前版本的 PostgreSQL 标准对象。在数据库集群初始化之后,不应该对 template0 做任何修改。通过告诉 CREATE DATABASE 使用 template0 而不是 template1 进行拷贝,你可以创建一个"纯净"的用户数据库,它不会包含任何 template1 里节点所附加的东西。这一点在恢复 pg_dump 转储的时候是非常方便的:转储脚本应该在一个纯洁的数据库中恢复以确保我们创建了被转储出的数据库中的正确内容, 而不和可能已经存在在 template1 中的对象相冲突。<br />
<br />
另一个不用 template1 而用 template0 去拷贝的原因是,拷贝 template0 时你可以指定新的编码和本地化设置,而拷贝 template1 时必须使用相同的设置。因为 template1 可能已经指定了编码和本地化设置,而 template0 却没有。<br />
<br />
要通过拷贝 template0 的方式创建一个数据库,使用命令:<br />
<br />
<pre>CREATE DATABASE dbname TEMPLATE template0;</pre><br />
<br />
这条命令是在 SQL 环境里的,另一个命令:<br />
<br />
<pre>createdb -T template0 dbname</pre><br />
<br />
是从shell里执行的。<br />
<br />
我们可以创建额外的模板数据库,而且实际上只要为 CREATE DATABASE 指定模版名字就可以拷贝集群中的任何一个数据库。不过,我们必需明白,这个功能并非一般性的"COPY DATABASE"工具。这里有个限制是源数据库在拷贝时不能连接任何会话。如果源数据库存在任何其他的连接,在使用 CREATE DATABASE 拷贝时就会失败,在拷贝进行中,试图连接到源数据库的操作都会被阻止。<br />
<br />
在 pg_database 里针对每个数据库有两个有用的标志:字段 datistemplate 和 datallowconn。datistemplate 表示该数据库是否准备用做 CREATE DATABASE 的模板。如果设置了这个标志,那么该数据库可以由任何有 CREATEDB 权限的用户克隆;如果没有设置,那么只有超级用户和该数据库的所有者可以克隆它。如果 datallowconn 为假,那么将不允许与该数据库发生任何新的连接(不过现有的会话不会因为把该标志设置为假而被中断)。template0 数据库通常被标记为 datallowconn = false 以避免对它的修改。template0 和 template1应该总是标记为datistemplate = true。<br />
<br />
<blockquote><br />
注意:除了 template1 在CREATE DATABASE 时是缺省的源数据库外,template1 和 template0 没有任何其他特殊状态。例如,你可以删除template1并根据template0重新创建以消除任何不利的影响。当你不小心污染了template1时推荐这种做法。(删除template1前,必须先在pg_database中设置其datistemplate属性为false。)数据库集群在初始化时还会自动创建postgres数据库。其做为用户和应用程序缺省连接的数据库。它只是template1的一个简单拷贝,必要时也可以删除和重建。<br />
</blockquote><br />
<br />
==数据库配置==<br />
<br />
回顾一下 Chapter 18,我们知道PostgreSQL 服务器提供了大量的运行时配置变量。你可以为许多这样的变量设置数据库相关的缺省值。<br />
<br />
比如,如果由于某种原因,你想关闭某个数据库上的 GEQO 优化器,通常情况下你会在所有数据库中关闭它,或者是保证每个连接过来的客户端都很小心地设置了 SET geqo TO off。要令这个设置在特定数据库里成为缺省,你可以执行下面的命令:<br />
<br />
<pre>ALTER DATABASE mydb SET geqo TO off;</pre><br />
<br />
这样就保存了设置(但不是立即生效)。在随后的连接中,看起来好像是在会话开始后马上调用了 SET geqo TO off;请注意用户仍然可以在该会话中更改这个设置;它只是缺省。要撤消某个设置,使用ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203509.1第二十一章2013-07-06T12:48:26Z<p>Horen: /* 模板数据库 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE 实际上是通过拷贝一个现有的数据库进行工作的。缺省情况下,它拷贝名字叫 template1 的标准系统数据库。 所以该数据库是创建新数据库的"模板"。如果你给 template1 增加对象,这些对象将被拷贝到随后创建的用户数据库中。这样的行为允许节点对数据库中的标准套件进行修改。 比如,如果你把过程语言 PL/pgSQL 安装到 template1 里,那么你在创建用户数据库的时候它们就会自动可得,而不需要额外的动作。<br />
<br />
还有第二个标准的系统数据库,叫 template0。 这个数据库包含和 template1 一开始时一样的数据内容,也就是说,只有当前版本的 PostgreSQL 标准对象。在数据库集群初始化之后,不应该对 template0 做任何修改。通过告诉 CREATE DATABASE 使用 template0 而不是 template1 进行拷贝,你可以创建一个"纯净"的用户数据库,它不会包含任何 template1 里节点所附加的东西。这一点在恢复 pg_dump 转储的时候是非常方便的:转储脚本应该在一个纯洁的数据库中恢复以确保我们创建了被转储出的数据库中的正确内容, 而不和可能已经存在在 template1 中的对象相冲突。<br />
<br />
另一个不用 template1 而用 template0 去拷贝的原因是,拷贝 template0 时你可以指定新的编码和本地化设置,而拷贝 template1 时必须使用相同的设置。因为 template1 可能已经指定了编码和本地化设置,而 template0 却没有。<br />
<br />
要通过拷贝 template0 的方式创建一个数据库,使用命令:<br />
<br />
<pre>CREATE DATABASE dbname TEMPLATE template0;</pre><br />
<br />
这条命令是在 SQL 环境里的,另一个命令:<br />
<br />
<pre>createdb -T template0 dbname</pre><br />
<br />
是从shell里执行的。<br />
<br />
我们可以创建额外的模板数据库,而且实际上只要为 CREATE DATABASE 指定模版名字就可以拷贝集群中的任何一个数据库。不过,我们必需明白,这个功能并非一般性的"COPY DATABASE"工具。这里有个限制是源数据库在拷贝时不能连接任何会话。如果源数据库存在任何其他的连接,在使用 CREATE DATABASE 拷贝时就会失败,在拷贝进行中,试图连接到源数据库的操作都会被阻止。<br />
<br />
在 pg_database 里针对每个数据库有两个有用的标志:字段 datistemplate 和 datallowconn。datistemplate 表示该数据库是否准备用做 CREATE DATABASE 的模板。如果设置了这个标志,那么该数据库可以由任何有 CREATEDB 权限的用户克隆;如果没有设置,那么只有超级用户和该数据库的所有者可以克隆它。如果 datallowconn 为假,那么将不允许与该数据库发生任何新的连接(不过现有的会话不会因为把该标志设置为假而被中断)。template0 数据库通常被标记为 datallowconn = false 以避免对它的修改。template0 和 template1应该总是标记为datistemplate = true。<br />
<br />
<blockquote><br />
注意:除了 template1 在CREATE DATABASE 时是缺省的源数据库外,template1 和 template0 没有任何其他特殊状态。例如,你可以删除template1并根据template0重新创建以消除任何不利的影响。当你不小心污染了template1时推荐这种做法。(删除template1前,必须先在pg_database中设置其datistemplate属性为false。)数据库集群在初始化时还会自动创建postgres数据库。其做为用户和应用程序缺省连接的数据库。它只是template1的一个简单拷贝,必要时也可以删除和重建。<br />
</blockquote><br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203499.1第二十一章2013-07-06T12:44:47Z<p>Horen: /* 模板数据库 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
<pre>CREATE DATABASE dbname TEMPLATE template0;</pre><br />
<br />
from the SQL environment, or:<br />
<br />
<pre>createdb -T template0 dbname</pre><br />
<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
<blockquote><br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
</blockquote><br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203469.1第二十一章2013-07-05T16:04:08Z<p>Horen: /* 创建数据库 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
为了创建数据库,必须先运行PostgreSQL服务器(参阅 Section 17.3)。<br />
<br />
创建数据库可以用 SQL 命令 CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
这里的 name 遵循SQL标识符的一般规则。当前角色自动成为此新数据库的所有者。以后删除这个数据库也是这个用户的特权(同时还会删除其中的所有对象, 即使那些对象有不同的所有者也这样。)<br />
<br />
创建数据库是一个受限制的操作。参阅 Section 20.2 获取如何赋以权限的信息。<br />
<br />
因为你需要连接到数据库服务器才能执行命令CREATE DATABASE,那么问题是任意节点的第一个数据库是怎样创建的?第一个数据库总是由initdb命令在初始化数据存储区的时候创建的。(参阅Section 17.2。)这个数据库叫postgres。 因此要创建第一个"真正"的数据库的时候你可以与postgres联接。<br />
<br />
第二个数据库,template1, 也是在数据库集群初始化时被创建的。每创建一个新的数据库时,实际上就是克隆了 template1 数据库。这就意味着你对 template1 做的任何修改都会传播到所有随后创建的数据库。正因如此,应该避免在 template1 数据库中创建任何对象,除非你想将它们传播到后面创建的所有数据库中。更多细节见Section 21.3。<br />
<br />
方便起见,你还可以用一个可以在 shell 中执行的程序来创建新数据库,createdb。<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb 没变什么魔术,它和postgres连接并执行 CREATE DATABASE 命令。createdb 的手册页包含使用它的细节。需要注意的是不带任何参数调用 createdb 将以当前用户名为名称创建数据库。<br />
<br />
<blockquote>注意: Chapter 19 包含有关如何限制某个用户可以连接的数据库的信息。</blockquote><br />
<br />
有时候你想为其他人创建一个数据库。并让那个人成为新数据库的所有者,这样他就可以自己配置和管理这个数据库。要实现这个目的,使用下列命令中的某一条:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
上面的是在 SQL 环境中执行的,或者<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
在shell中执行。<br />
<br />
只有超级用户才可以为其他用户(并且该超级用户不能是该用户的成员)创建数据库。<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
CREATE DATABASE dbname TEMPLATE template0;<br />
from the SQL environment, or:<br />
<br />
createdb -T template0 dbname<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203459.1第二十一章2013-07-05T16:00:48Z<p>Horen: /* 创建数据库 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
In order to create a database, the PostgreSQL server must be up and running (see Section 17.3).<br />
<br />
Databases are created with the SQL command CREATE DATABASE:<br />
<br />
<pre>CREATE DATABASE name;</pre><br />
<br />
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).<br />
<br />
The creation of databases is a restricted operation. See Section 20.2 for how to grant permission.<br />
<br />
Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See Section 17.2.) This database is called postgres. So to create the first "ordinary" database you can connect to postgres.<br />
<br />
A second database, template1, is also created during database cluster initialization. Whenever a new database is created within the cluster, template1 is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. Because of this, avoid creating objects in template1 unless you want them propagated to every newly created database. More details appear in Section 21.3.<br />
<br />
As a convenience, there is a program you can execute from the shell to create new databases, createdb.<br />
<br />
<pre>createdb dbname</pre><br />
<br />
createdb does no magic. It connects to the postgres database and issues the CREATE DATABASE command, exactly as described above. The createdb reference page contains the invocation details. Note that createdb without any arguments will create a database with the current user name.<br />
<br />
<blockquote>Note: Chapter 19 contains information about how to restrict who can connect to a given database.</blockquote><br />
<br />
Sometimes you want to create a database for someone else, and have him become the owner of the new database, so he can configure and manage it himself. To achieve that, use one of the following commands:<br />
<br />
<pre>CREATE DATABASE dbname OWNER rolename;</pre><br />
<br />
from the SQL environment, or:<br />
<br />
<pre>createdb -O rolename dbname</pre><br />
<br />
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
CREATE DATABASE dbname TEMPLATE template0;<br />
from the SQL environment, or:<br />
<br />
createdb -T template0 dbname<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203449.1第二十一章2013-07-05T15:56:38Z<p>Horen: /* 概述 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
数据库是一些SQL对象("数据库对象")的集合;通常每个数据库对象(表,函数等等)属于并且只属于一个数据库。(不过有几个系统表,比如 pg_database,属于整个集群并且可以在集群之内的每个数据库里访问。)更准确地说,一个数据库是一个模式的集合,而模式包含表,函数等等。因此完整的层次是:服务器,数据库,模式,表(或者其他类型对象,比如函数)。<br />
<br />
在与数据库服务器联接的时候,客户应该在它的联接请求里面带有它想与之联接的数据库名称。不允许在一次联接里面对多个数据库访问.不过,没有限制一个客户与同一个或者其他数据库可以建立的联接数量.数据库是物理上相互隔离的,对它们的访问控制是在联接层次进行控制的。如果一个PostgreSQL 服务器实例用于承载那些应该分隔并且相互之间并不知晓的用户和项目,那么我们建议把它们放在不同的数据库里。如果项目或者用户是相互关联的,并且可以相互使用对方的资源,那么应该把它们放在同一个数据库里,但可能是不同的模式里。模式只是一个纯粹的逻辑结构,谁能访问某个模式由权限系统控制。有关管理模式的更多信息在 Section 5.7 里。<br />
<br />
数据库是使用 CREATE DATABASE 命令创建的(参阅 Section 21.2), 用 DROP DATABASE 命令删除(参阅 Section 21.5)。要判断现有数据库的集合,检查系统表 pg_database,比如<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
psql 程序的 \l 元命令和 -l 命令行选项也可以用来列出现存数据库。<br />
<br />
<blockquote>注意: SQL 标准把数据库称作"目录(catalogs)",不过这两个东西实际上没有什么区别。</blockquote><br />
<br />
==创建数据库==<br />
<br />
In order to create a database, the PostgreSQL server must be up and running (see Section 17.3).<br />
<br />
Databases are created with the SQL command CREATE DATABASE:<br />
<br />
CREATE DATABASE name;<br />
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).<br />
<br />
The creation of databases is a restricted operation. See Section 20.2 for how to grant permission.<br />
<br />
Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See Section 17.2.) This database is called postgres. So to create the first "ordinary" database you can connect to postgres.<br />
<br />
A second database, template1, is also created during database cluster initialization. Whenever a new database is created within the cluster, template1 is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. Because of this, avoid creating objects in template1 unless you want them propagated to every newly created database. More details appear in Section 21.3.<br />
<br />
As a convenience, there is a program you can execute from the shell to create new databases, createdb.<br />
<br />
createdb dbname<br />
createdb does no magic. It connects to the postgres database and issues the CREATE DATABASE command, exactly as described above. The createdb reference page contains the invocation details. Note that createdb without any arguments will create a database with the current user name.<br />
<br />
Note: Chapter 19 contains information about how to restrict who can connect to a given database.<br />
Sometimes you want to create a database for someone else, and have him become the owner of the new database, so he can configure and manage it himself. To achieve that, use one of the following commands:<br />
<br />
CREATE DATABASE dbname OWNER rolename;<br />
from the SQL environment, or:<br />
<br />
createdb -O rolename dbname<br />
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
CREATE DATABASE dbname TEMPLATE template0;<br />
from the SQL environment, or:<br />
<br />
createdb -T template0 dbname<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203439.1第二十一章2013-07-05T15:55:04Z<p>Horen: /* 概述 */</p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
A database is a named collection of SQL objects ("database objects"). Generally, every database object (tables, functions, etc.) belongs to one and only one database. (However there are a few system catalogs, for example pg_database, that belong to a whole cluster and are accessible from each database within the cluster.) More accurately, a database is a collection of schemas and the schemas contain the tables, functions, etc. So the full hierarchy is: server, database, schema, table (or some other kind of object, such as a function).<br />
<br />
When connecting to the database server, a client must specify in its connection request the name of the database it wants to connect to. It is not possible to access more than one database per connection. However, an application is not restricted in the number of connections it opens to the same or other databases. Databases are physically separated and access control is managed at the connection level. If one PostgreSQL server instance is to house projects or users that should be separate and for the most part unaware of each other, it is therefore recommendable to put them into separate databases. If the projects or users are interrelated and should be able to use each other's resources, they should be put in the same database but possibly into separate schemas. Schemas are a purely logical structure and who can access what is managed by the privilege system. More information about managing schemas is in Section 5.7.<br />
<br />
Databases are created with the CREATE DATABASE command (see Section 21.2) and destroyed with the DROP DATABASE command (see Section 21.5). To determine the set of existing databases, examine the pg_database system catalog, for example<br />
<br />
<pre>SELECT datname FROM pg_database;</pre><br />
<br />
The psql program's \l meta-command and -l command-line option are also useful for listing the existing databases.<br />
<br />
<blockquote>Note: The SQL standard calls databases "catalogs", but there is no difference in practice.</blockquote><br />
<br />
==创建数据库==<br />
<br />
In order to create a database, the PostgreSQL server must be up and running (see Section 17.3).<br />
<br />
Databases are created with the SQL command CREATE DATABASE:<br />
<br />
CREATE DATABASE name;<br />
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).<br />
<br />
The creation of databases is a restricted operation. See Section 20.2 for how to grant permission.<br />
<br />
Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See Section 17.2.) This database is called postgres. So to create the first "ordinary" database you can connect to postgres.<br />
<br />
A second database, template1, is also created during database cluster initialization. Whenever a new database is created within the cluster, template1 is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. Because of this, avoid creating objects in template1 unless you want them propagated to every newly created database. More details appear in Section 21.3.<br />
<br />
As a convenience, there is a program you can execute from the shell to create new databases, createdb.<br />
<br />
createdb dbname<br />
createdb does no magic. It connects to the postgres database and issues the CREATE DATABASE command, exactly as described above. The createdb reference page contains the invocation details. Note that createdb without any arguments will create a database with the current user name.<br />
<br />
Note: Chapter 19 contains information about how to restrict who can connect to a given database.<br />
Sometimes you want to create a database for someone else, and have him become the owner of the new database, so he can configure and manage it himself. To achieve that, use one of the following commands:<br />
<br />
CREATE DATABASE dbname OWNER rolename;<br />
from the SQL environment, or:<br />
<br />
createdb -O rolename dbname<br />
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
CREATE DATABASE dbname TEMPLATE template0;<br />
from the SQL environment, or:<br />
<br />
createdb -T template0 dbname<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203429.1第二十一章2013-07-05T15:51:23Z<p>Horen: </p>
<hr />
<div>'''数据库管理'''<br />
----<br />
每个运行PostgreSQL服务器的实例都管理着一个或多个数据库。因此,在组织SQL("数据库对象")对象的层次中,数据库位于最顶层。本章描述数据库的属性,以及如何创建,管理和删除它们。<br />
<br />
==概述==<br />
<br />
A database is a named collection of SQL objects ("database objects"). Generally, every database object (tables, functions, etc.) belongs to one and only one database. (However there are a few system catalogs, for example pg_database, that belong to a whole cluster and are accessible from each database within the cluster.) More accurately, a database is a collection of schemas and the schemas contain the tables, functions, etc. So the full hierarchy is: server, database, schema, table (or some other kind of object, such as a function).<br />
<br />
When connecting to the database server, a client must specify in its connection request the name of the database it wants to connect to. It is not possible to access more than one database per connection. However, an application is not restricted in the number of connections it opens to the same or other databases. Databases are physically separated and access control is managed at the connection level. If one PostgreSQL server instance is to house projects or users that should be separate and for the most part unaware of each other, it is therefore recommendable to put them into separate databases. If the projects or users are interrelated and should be able to use each other's resources, they should be put in the same database but possibly into separate schemas. Schemas are a purely logical structure and who can access what is managed by the privilege system. More information about managing schemas is in Section 5.7.<br />
<br />
Databases are created with the CREATE DATABASE command (see Section 21.2) and destroyed with the DROP DATABASE command (see Section 21.5). To determine the set of existing databases, examine the pg_database system catalog, for example<br />
<br />
SELECT datname FROM pg_database;<br />
The psql program's \l meta-command and -l command-line option are also useful for listing the existing databases.<br />
<br />
Note: The SQL standard calls databases "catalogs", but there is no difference in practice.<br />
<br />
==创建数据库==<br />
<br />
In order to create a database, the PostgreSQL server must be up and running (see Section 17.3).<br />
<br />
Databases are created with the SQL command CREATE DATABASE:<br />
<br />
CREATE DATABASE name;<br />
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).<br />
<br />
The creation of databases is a restricted operation. See Section 20.2 for how to grant permission.<br />
<br />
Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See Section 17.2.) This database is called postgres. So to create the first "ordinary" database you can connect to postgres.<br />
<br />
A second database, template1, is also created during database cluster initialization. Whenever a new database is created within the cluster, template1 is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. Because of this, avoid creating objects in template1 unless you want them propagated to every newly created database. More details appear in Section 21.3.<br />
<br />
As a convenience, there is a program you can execute from the shell to create new databases, createdb.<br />
<br />
createdb dbname<br />
createdb does no magic. It connects to the postgres database and issues the CREATE DATABASE command, exactly as described above. The createdb reference page contains the invocation details. Note that createdb without any arguments will create a database with the current user name.<br />
<br />
Note: Chapter 19 contains information about how to restrict who can connect to a given database.<br />
Sometimes you want to create a database for someone else, and have him become the owner of the new database, so he can configure and manage it himself. To achieve that, use one of the following commands:<br />
<br />
CREATE DATABASE dbname OWNER rolename;<br />
from the SQL environment, or:<br />
<br />
createdb -O rolename dbname<br />
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
CREATE DATABASE dbname TEMPLATE template0;<br />
from the SQL environment, or:<br />
<br />
createdb -T template0 dbname<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203419.1第二十一章2013-07-05T15:48:44Z<p>Horen: </p>
<hr />
<div>'''数据库管理'''<br />
Every instance of a running PostgreSQL server manages one or more databases. Databases are therefore the topmost hierarchical level for organizing SQL objects ("database objects"). This chapter describes the properties of databases, and how to create, manage, and destroy them.<br />
----<br />
<br />
==概述==<br />
<br />
A database is a named collection of SQL objects ("database objects"). Generally, every database object (tables, functions, etc.) belongs to one and only one database. (However there are a few system catalogs, for example pg_database, that belong to a whole cluster and are accessible from each database within the cluster.) More accurately, a database is a collection of schemas and the schemas contain the tables, functions, etc. So the full hierarchy is: server, database, schema, table (or some other kind of object, such as a function).<br />
<br />
When connecting to the database server, a client must specify in its connection request the name of the database it wants to connect to. It is not possible to access more than one database per connection. However, an application is not restricted in the number of connections it opens to the same or other databases. Databases are physically separated and access control is managed at the connection level. If one PostgreSQL server instance is to house projects or users that should be separate and for the most part unaware of each other, it is therefore recommendable to put them into separate databases. If the projects or users are interrelated and should be able to use each other's resources, they should be put in the same database but possibly into separate schemas. Schemas are a purely logical structure and who can access what is managed by the privilege system. More information about managing schemas is in Section 5.7.<br />
<br />
Databases are created with the CREATE DATABASE command (see Section 21.2) and destroyed with the DROP DATABASE command (see Section 21.5). To determine the set of existing databases, examine the pg_database system catalog, for example<br />
<br />
SELECT datname FROM pg_database;<br />
The psql program's \l meta-command and -l command-line option are also useful for listing the existing databases.<br />
<br />
Note: The SQL standard calls databases "catalogs", but there is no difference in practice.<br />
<br />
==创建数据库==<br />
<br />
In order to create a database, the PostgreSQL server must be up and running (see Section 17.3).<br />
<br />
Databases are created with the SQL command CREATE DATABASE:<br />
<br />
CREATE DATABASE name;<br />
where name follows the usual rules for SQL identifiers. The current role automatically becomes the owner of the new database. It is the privilege of the owner of a database to remove it later (which also removes all the objects in it, even if they have a different owner).<br />
<br />
The creation of databases is a restricted operation. See Section 20.2 for how to grant permission.<br />
<br />
Since you need to be connected to the database server in order to execute the CREATE DATABASE command, the question remains how the first database at any given site can be created. The first database is always created by the initdb command when the data storage area is initialized. (See Section 17.2.) This database is called postgres. So to create the first "ordinary" database you can connect to postgres.<br />
<br />
A second database, template1, is also created during database cluster initialization. Whenever a new database is created within the cluster, template1 is essentially cloned. This means that any changes you make in template1 are propagated to all subsequently created databases. Because of this, avoid creating objects in template1 unless you want them propagated to every newly created database. More details appear in Section 21.3.<br />
<br />
As a convenience, there is a program you can execute from the shell to create new databases, createdb.<br />
<br />
createdb dbname<br />
createdb does no magic. It connects to the postgres database and issues the CREATE DATABASE command, exactly as described above. The createdb reference page contains the invocation details. Note that createdb without any arguments will create a database with the current user name.<br />
<br />
Note: Chapter 19 contains information about how to restrict who can connect to a given database.<br />
Sometimes you want to create a database for someone else, and have him become the owner of the new database, so he can configure and manage it himself. To achieve that, use one of the following commands:<br />
<br />
CREATE DATABASE dbname OWNER rolename;<br />
from the SQL environment, or:<br />
<br />
createdb -O rolename dbname<br />
from the shell. Only the superuser is allowed to create a database for someone else (that is, for a role you are not a member of).<br />
<br />
==模板数据库==<br />
<br />
CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created.<br />
<br />
There is a second standard system database named template0. This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a "virgin" user database that contains none of the site-local additions in template1. This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a virgin database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.<br />
<br />
Another common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.<br />
<br />
To create a database by copying template0, use:<br />
<br />
CREATE DATABASE dbname TEMPLATE template0;<br />
from the SQL environment, or:<br />
<br />
createdb -T template0 dbname<br />
from the shell.<br />
<br />
It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. It is important to understand, however, that this is not (yet) intended as a general-purpose "COPY DATABASE" facility. The principal limitation is that no other sessions can be connected to the source database while it is being copied. CREATE DATABASE will fail if any other connection exists when it starts; during the copy operation, new connections to the source database are prevented.<br />
<br />
Two useful flags exist in pg_database for each database: the columns datistemplate and datallowconn. datistemplate can be set to indicate that a database is intended as a template for CREATE DATABASE. If this flag is set, the database can be cloned by any user with CREATEDB privileges; if it is not set, only superusers and the owner of the database can clone it. If datallowconn is false, then no new connections to that database will be allowed (but existing sessions are not terminated simply by setting the flag false). The template0 database is normally marked datallowconn = false to prevent its modification. Both template0 and template1 should always be marked with datistemplate = true.<br />
<br />
Note: template1 and template0 do not have any special status beyond the fact that the name template1 is the default source database name for CREATE DATABASE. For example, one could drop template1 and recreate it from template0 without any ill effects. This course of action might be advisable if one has carelessly added a bunch of junk in template1. (To delete template1, it must have pg_database.datistemplate = false.)<br />
The postgres database is also created when a database cluster is initialized. This database is meant as a default database for users and applications to connect to. It is simply a copy of template1 and can be dropped and recreated if necessary.<br />
<br />
==数据库配置==<br />
<br />
Recall from Chapter 18 that the PostgreSQL server provides a large number of run-time configuration variables. You can set database-specific default values for many of these settings.<br />
<br />
For example, if for some reason you want to disable the GEQO optimizer for a given database, you'd ordinarily have to either disable it for all databases or make sure that every connecting client is careful to issue SET geqo TO off. To make this setting the default within a particular database, you can execute the command:<br />
<br />
ALTER DATABASE mydb SET geqo TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections to this database it will appear as though SET geqo TO off; had been executed just before the session started. Note that users can still alter this setting during their sessions; it will only be the default. To undo any such setting, use ALTER DATABASE dbname RESET varname.<br />
<br />
==删除数据库==<br />
<br />
Databases are destroyed with the command DROP DATABASE:<br />
<br />
DROP DATABASE name;<br />
Only the owner of the database, or a superuser, can drop a database. Dropping a database removes all objects that were contained within the database. The destruction of a database cannot be undone.<br />
<br />
You cannot execute the DROP DATABASE command while connected to the victim database. You can, however, be connected to any other database, including the template1 database. template1 would be the only option for dropping the last user database of a given cluster.<br />
<br />
For convenience, there is also a shell program to drop databases, dropdb:<br />
<br />
dropdb dbname<br />
(Unlike createdb, it is not the default action to drop the database with the current user name.)<br />
<br />
==表空间==<br />
<br />
Tablespaces in PostgreSQL allow database administrators to define locations in the file system where the files representing database objects can be stored. Once created, a tablespace can be referred to by name when creating database objects.<br />
<br />
By using tablespaces, an administrator can control the disk layout of a PostgreSQL installation. This is useful in at least two ways. First, if the partition or volume on which the cluster was initialized runs out of space and cannot be extended, a tablespace can be created on a different partition and used until the system can be reconfigured.<br />
<br />
Second, tablespaces allow an administrator to use knowledge of the usage pattern of database objects to optimize performance. For example, an index which is very heavily used can be placed on a very fast, highly available disk, such as an expensive solid state device. At the same time a table storing archived data which is rarely used or not performance critical could be stored on a less expensive, slower disk system.<br />
<br />
To define a tablespace, use the CREATE TABLESPACE command, for example::<br />
<br />
CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';<br />
The location must be an existing, empty directory that is owned by the PostgreSQL operating system user. All objects subsequently created within the tablespace will be stored in files underneath this directory.<br />
<br />
Note: There is usually not much point in making more than one tablespace per logical file system, since you cannot control the location of individual files within a logical file system. However, PostgreSQL does not enforce any such limitation, and indeed it is not directly aware of the file system boundaries on your system. It just stores files in the directories you tell it to use.<br />
Creation of the tablespace itself must be done as a database superuser, but after that you can allow ordinary database users to use it. To do that, grant them the CREATE privilege on it.<br />
<br />
Tables, indexes, and entire databases can be assigned to particular tablespaces. To do so, a user with the CREATE privilege on a given tablespace must pass the tablespace name as a parameter to the relevant command. For example, the following creates a table in the tablespace space1:<br />
<br />
CREATE TABLE foo(i int) TABLESPACE space1;<br />
Alternatively, use the default_tablespace parameter:<br />
<br />
SET default_tablespace = space1;<br />
CREATE TABLE foo(i int);<br />
When default_tablespace is set to anything but an empty string, it supplies an implicit TABLESPACE clause for CREATE TABLE and CREATE INDEX commands that do not have an explicit one.<br />
<br />
There is also a temp_tablespaces parameter, which determines the placement of temporary tables and indexes, as well as temporary files that are used for purposes such as sorting large data sets. This can be a list of tablespace names, rather than only one, so that the load associated with temporary objects can be spread over multiple tablespaces. A random member of the list is picked each time a temporary object is to be created.<br />
<br />
The tablespace associated with a database is used to store the system catalogs of that database. Furthermore, it is the default tablespace used for tables, indexes, and temporary files created within the database, if no TABLESPACE clause is given and no other selection is specified by default_tablespace or temp_tablespaces (as appropriate). If a database is created without specifying a tablespace for it, it uses the same tablespace as the template database it is copied from.<br />
<br />
Two tablespaces are automatically created when the database cluster is initialized. The pg_global tablespace is used for shared system catalogs. The pg_default tablespace is the default tablespace of the template1 and template0 databases (and, therefore, will be the default tablespace for other databases as well, unless overridden by a TABLESPACE clause in CREATE DATABASE).<br />
<br />
Once created, a tablespace can be used from any database, provided the requesting user has sufficient privilege. This means that a tablespace cannot be dropped until all objects in all databases using the tablespace have been removed.<br />
<br />
To remove an empty tablespace, use the DROP TABLESPACE command.<br />
<br />
To determine the set of existing tablespaces, examine the pg_tablespace system catalog, for example<br />
<br />
SELECT spcname FROM pg_tablespace;<br />
The psql program's \db meta-command is also useful for listing the existing tablespaces.<br />
<br />
PostgreSQL makes use of symbolic links to simplify the implementation of tablespaces. This means that tablespaces can be used only on systems that support symbolic links.<br />
<br />
The directory $PGDATA/pg_tblspc contains symbolic links that point to each of the non-built-in tablespaces defined in the cluster. Although not recommended, it is possible to adjust the tablespace layout by hand by redefining these links. Two warnings: do not do so while the server is running; and after you restart the server, update the pg_tablespace catalog with the new locations. (If you do not, pg_dump will continue to output the old tablespace locations.)</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203409.1第二十章2013-07-05T15:42:18Z<p>Horen: /* 角色成员 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : 角色若要创建更多的角色必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEROLE. 拥有 CREATEROLE 权限的角色可以修改和删除其他角色,同时还可以授予或撤销它们的成员属性. 但是,若要创建、修改、删除或者变更超级用户角色的成员属性需要有超级用户权限,仅仅 CREATEROLE 是不够的.<br />
<br />
;启动复制 : 角色若要启动流复制必须在创建该角色时明确给出权限。用来启动流复制的角色必须拥有 LOGIN 权限。要创建这样的角色,使用命令 CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : 口令只有在客户端试图连接数据库且客户短认证方法要求其提供口令时才有用。口令本身和 md5 认证方法可使口令生效。数据库口令和操作系统口令是独立的。创建角色并指定口令使用命令 CREATE ROLE name PASSWORD 'string'.<br />
<br />
角色属性可以使用命令 ALTER ROLE 修改。关于 CREATE ROLE 和 ALTER ROLE 命令的更多信息,请查看参考页。<br />
<br />
<blockquote>'''Tip:''' 推荐在创建一个拥有 CREATEDB 和 CREATEROLE 权限但不是超级用户的角色,并用它完成数据库和角色的日常管理工作。这种方法避免了那些本不需要超级用户权限的误操作。</blockquote><br />
<br />
可以为角色指定运行时缺省配置,这些配置在第18章有描述。例如,处于某种原因,你想让角色连接到数据库时自动关闭索引扫瞄(提示:并不是个好主意),可以使用命名<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
这样就保存了设置(但不是立即生效)。只是该角色再连接数据库时生效,看起来好像是开启会话前执行了命令 SET enable_indexscan TO off 一样。你也可以在会话中修改该设置,它只是默认行为。若要取消某个默认设置使用命令 ALTER ROLE rolename RESET varname. 给没有 LOGIN 权限的角色设置缺省行为是不会起作用的,因为它们从不会被调用。<br />
<br />
== 角色成员 ==<br />
将用户分组通常可以简化权限管理:这样,权限的授予和撤消可以作用于整个组。PostgreSQL 通过创建一个角色来代表某个组,然后将该组的成员权限授予给其他独立的用户角色。<br />
<br />
建立一个组角色,首先要创建角色:<br />
<br />
<pre>CREATE ROLE name;</pre><br />
<br />
通常,作为组角色的角色没有 LOGIN 属性,当然你也可以设置它。<br />
<br />
如果组角色存在,可以使用 GRANT 和 REVOKE 命令添加和删除成员:<br />
<br />
<pre>GRANT group_role TO role1, ... ;<br />
REVOKE group_role FROM role1, ... ;</pre><br />
<br />
你还可以赋予成员权限给其它组的角色(因为组角色和非组角色没有实质的区别)。数据库不允许建立循环的成员关系。同时,还不允许授予成员权限给 PUBLIC。<br />
<br />
组内成员通过两种方式使用组角色的权限。第一,一个组的每个成员都可以明确用 SET ROLE 临时"变成"组角色。在这个状态下,数据库会话具有该组角色的权限, 而不是原始的登录角色权限,这个时候创建的数据库对象被认为是由组角色拥有,而不是登录角色。 第二,拥有 INHERIT 属性的角色成员自动具有它们所属角色组的权限。 例如,假如我们做了下面的事情:<br />
<br />
<pre><br />
CREATE ROLE joe LOGIN INHERIT;<br />
CREATE ROLE admin NOINHERIT;<br />
CREATE ROLE wheel NOINHERIT;<br />
GRANT admin TO joe;<br />
GRANT wheel TO admin;<br />
</pre><br />
<br />
一旦以角色 joe 连接数据库,该数据库会话便拥有直接赋予 joe 的权限加上任何赋予 admin 的权限,因为 joe "继承"了 admin 的权限。但是,赋予 wheel 的权限不可用,因为即使 joe 是 wheel 的一个间接成员,但该成员关系是通过 admin 传递过来的, 而admin有 NOINHERIT 属性。接着:<br />
<br />
<pre>SET ROLE admin;</pre><br />
<br />
该会话将只拥有那些赋予 admin 的权限,而不包括那些赋予 joe 的权限。 然后:<br />
<br />
<pre>SET ROLE wheel;</pre><br />
<br />
该会话讲只能使用赋予 wheel 的权限,而不包括赋予 joe 或者 admine 的权限。原来的权限可以用下列之一恢复:<br />
<br />
<pre><br />
SET ROLE joe;<br />
SET ROLE NONE;<br />
RESET ROLE;<br />
</pre><br />
<br />
注意: SET ROLE 命令总是允许选取任意登录角色直接或者间接所在的组角色。 因此,在上面的例子里,没必要在变成 wheel 之前先变成 admin。<br />
<br />
注意: 在 SQL 标准里,用户和角色之间有一个明确的区别, 用户并不会自动继承权限,而角色可以。这个行为在 PostgreSQL 里面可以通过给予那些当作 SQL 角色使用的角色以 INHERIT 属性, 而给予当作 SQL 用户使用的角色以 NOINHERIT 属性来实现。 不过,PostgreSQL 缺省是给予所有角色 INHERIT 属性, 目的是和 8.1 之前的版本向下兼容,那些版本里,用户总是能使用他们所在组被赋予的权限。<br />
<br />
角色属性 LOGIN,SUPERUSER 和 CREATEROLE 可以认为是特殊的权限,但是它们从来不会像数据库对象上的普通权限那样继承。 你必须真实地 SET ROLE 到一个特定的角色,这个角色应该是拥有这些属性的角色, 然后才能利用这些属性。继续上面的例子,我们也可以选择给 admin 角色赋予 CREATEDB 和 CREATEROLE 权限。 但是,已经以 joe 连接的绘话不会立即拥有这些权限,只有在 SET ROLE admin 之后才有。<br />
<br />
要删除一个组角色,使用命令 DROP ROLE:<br />
<br />
<pre>DROP ROLE name;</pre><br />
<br />
删除组角色之后,任何在组角色里面的成员关系都会自动撤消(但是成员角色自己则不会受影响)。 不过,请注意任何组角色拥有的对象都必须首先删除或者赋予其它所有者;并且任何赋给该组角色的权限都必须撤消。<br />
<br />
== 函数和触发器 ==<br />
函数和触发器允许用户向后端服务器插入代码,这样其他用户可能无意中执行到这些代码。 因此,两种机制都可以让用户相当容易地给别人设置 "Trojan horse(木马)", 唯一的有效防护就是严格控制谁可以定义函数。<br />
<br />
后端服务器里面的函数都是以启动数据库服务器守护进程的操作系统权限执行的。如果编写该函数的编程语言允许无检查的内存访问,服务器内部数据结构都可以被修改。因此,除了其他问题外,这样的函数可以绕过任何系统访问控制。 允许这样访问的函数语言都被认为是"不可信的(untrusted)", PostgreSQL 只允许超级用户使用这样的语言书写函数。</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E4%B8%80%E7%AB%A0&diff=203339.1第二十一章2013-07-03T15:24:50Z<p>Horen: Created page with "'''数据库管理''' ----"</p>
<hr />
<div>'''数据库管理'''<br />
----</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203329.1第二十章2013-07-03T15:21:51Z<p>Horen: /* 函数和触发器安全 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : 角色若要创建更多的角色必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEROLE. 拥有 CREATEROLE 权限的角色可以修改和删除其他角色,同时还可以授予或撤销它们的成员属性. 但是,若要创建、修改、删除或者变更超级用户角色的成员属性需要有超级用户权限,仅仅 CREATEROLE 是不够的.<br />
<br />
;启动复制 : 角色若要启动流复制必须在创建该角色时明确给出权限。用来启动流复制的角色必须拥有 LOGIN 权限。要创建这样的角色,使用命令 CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : 口令只有在客户端试图连接数据库且客户短认证方法要求其提供口令时才有用。口令本身和 md5 认证方法可使口令生效。数据库口令和操作系统口令是独立的。创建角色并指定口令使用命令 CREATE ROLE name PASSWORD 'string'.<br />
<br />
角色属性可以使用命令 ALTER ROLE 修改。关于 CREATE ROLE 和 ALTER ROLE 命令的更多信息,请查看参考页。<br />
<br />
<blockquote>'''Tip:''' 推荐在创建一个拥有 CREATEDB 和 CREATEROLE 权限但不是超级用户的角色,并用它完成数据库和角色的日常管理工作。这种方法避免了那些本不需要超级用户权限的误操作。</blockquote><br />
<br />
可以为角色指定运行时缺省配置,这些配置在第18章有描述。例如,处于某种原因,你想让角色连接到数据库时自动关闭索引扫瞄(提示:并不是个好主意),可以使用命名<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
这样就保存了设置(但不是立即生效)。只是该角色再连接数据库时生效,看起来好像是开启会话前执行了命令 SET enable_indexscan TO off 一样。你也可以在会话中修改该设置,它只是默认行为。若要取消某个默认设置使用命令 ALTER ROLE rolename RESET varname. 给没有 LOGIN 权限的角色设置缺省行为是不会起作用的,因为它们从不会被调用。<br />
<br />
== 角色成员 ==<br />
It is frequently convenient to group users together to ease management of privileges: that way, privileges can be granted to, or revoked from, a group as a whole. In PostgreSQL this is done by creating a role that represents the group, and then granting membership in the group role to individual user roles.<br />
<br />
To set up a group role, first create the role:<br />
<br />
<pre>CREATE ROLE name;</pre><br />
Typically a role being used as a group would not have the LOGIN attribute, though you can set it if you wish.<br />
<br />
Once the group role exists, you can add and remove members using the GRANT and REVOKE commands:<br />
<br />
<pre>GRANT group_role TO role1, ... ;<br />
REVOKE group_role FROM role1, ... ;</pre><br />
<br />
You can grant membership to other group roles, too (since there isn't really any distinction between group roles and non-group roles). The database will not let you set up circular membership loops. Also, it is not permitted to grant membership in a role to PUBLIC.<br />
<br />
The members of a group role can use the privileges of the role in two ways. First, every member of a group can explicitly do SET ROLE to temporarily "become" the group role. In this state, the database session has access to the privileges of the group role rather than the original login role, and any database objects created are considered owned by the group role not the login role. Second, member roles that have the INHERIT attribute automatically have use of the privileges of roles of which they are members, including any privileges inherited by those roles. As an example, suppose we have done:<br />
<br />
<pre><br />
CREATE ROLE joe LOGIN INHERIT;<br />
CREATE ROLE admin NOINHERIT;<br />
CREATE ROLE wheel NOINHERIT;<br />
GRANT admin TO joe;<br />
GRANT wheel TO admin;<br />
</pre><br />
<br />
Immediately after connecting as role joe, a database session will have use of privileges granted directly to joe plus any privileges granted to admin, because joe "inherits" admin's privileges. However, privileges granted to wheel are not available, because even though joe is indirectly a member of wheel, the membership is via admin which has the NOINHERIT attribute. After:<br />
<br />
<pre>SET ROLE admin;</pre><br />
the session would have use of only those privileges granted to admin, and not those granted to joe. After:<br />
<br />
<pre>SET ROLE wheel;</pre><br />
the session would have use of only those privileges granted to wheel, and not those granted to either joe or admin. The original privilege state can be restored with any of:<br />
<br />
<pre><br />
SET ROLE joe;<br />
SET ROLE NONE;<br />
RESET ROLE;<br />
</pre><br />
<br />
Note: The SET ROLE command always allows selecting any role that the original login role is directly or indirectly a member of. Thus, in the above example, it is not necessary to become admin before becoming wheel.<br />
Note: In the SQL standard, there is a clear distinction between users and roles, and users do not automatically inherit privileges while roles do. This behavior can be obtained in PostgreSQL by giving roles being used as SQL roles the INHERIT attribute, while giving roles being used as SQL users the NOINHERIT attribute. However, PostgreSQL defaults to giving all roles the INHERIT attribute, for backward compatibility with pre-8.1 releases in which users always had use of permissions granted to groups they were members of.<br />
The role attributes LOGIN, SUPERUSER, CREATEDB, and CREATEROLE can be thought of as special privileges, but they are never inherited as ordinary privileges on database objects are. You must actually SET ROLE to a specific role having one of these attributes in order to make use of the attribute. Continuing the above example, we might choose to grant CREATEDB and CREATEROLE to the admin role. Then a session connecting as role joe would not have these privileges immediately, only after doing SET ROLE admin.<br />
<br />
To destroy a group role, use DROP ROLE:<br />
<br />
<pre>DROP ROLE name;</pre><br />
<br />
Any memberships in the group role are automatically revoked (but the member roles are not otherwise affected). Note however that any objects owned by the group role must first be dropped or reassigned to other owners; and any permissions granted to the group role must be revoked.<br />
<br />
== 函数和触发器 ==<br />
函数和触发器允许用户向后端服务器插入代码,这样其他用户可能无意中执行到这些代码。 因此,两种机制都可以让用户相当容易地给别人设置 "Trojan horse(木马)", 唯一的有效防护就是严格控制谁可以定义函数。<br />
<br />
后端服务器里面的函数都是以启动数据库服务器守护进程的操作系统权限执行的。如果编写该函数的编程语言允许无检查的内存访问,服务器内部数据结构都可以被修改。因此,除了其他问题外,这样的函数可以绕过任何系统访问控制。 允许这样访问的函数语言都被认为是"不可信的(untrusted)", PostgreSQL 只允许超级用户使用这样的语言书写函数。</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203319.1第二十章2013-07-03T15:07:50Z<p>Horen: /* 角色成员 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : 角色若要创建更多的角色必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEROLE. 拥有 CREATEROLE 权限的角色可以修改和删除其他角色,同时还可以授予或撤销它们的成员属性. 但是,若要创建、修改、删除或者变更超级用户角色的成员属性需要有超级用户权限,仅仅 CREATEROLE 是不够的.<br />
<br />
;启动复制 : 角色若要启动流复制必须在创建该角色时明确给出权限。用来启动流复制的角色必须拥有 LOGIN 权限。要创建这样的角色,使用命令 CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : 口令只有在客户端试图连接数据库且客户短认证方法要求其提供口令时才有用。口令本身和 md5 认证方法可使口令生效。数据库口令和操作系统口令是独立的。创建角色并指定口令使用命令 CREATE ROLE name PASSWORD 'string'.<br />
<br />
角色属性可以使用命令 ALTER ROLE 修改。关于 CREATE ROLE 和 ALTER ROLE 命令的更多信息,请查看参考页。<br />
<br />
<blockquote>'''Tip:''' 推荐在创建一个拥有 CREATEDB 和 CREATEROLE 权限但不是超级用户的角色,并用它完成数据库和角色的日常管理工作。这种方法避免了那些本不需要超级用户权限的误操作。</blockquote><br />
<br />
可以为角色指定运行时缺省配置,这些配置在第18章有描述。例如,处于某种原因,你想让角色连接到数据库时自动关闭索引扫瞄(提示:并不是个好主意),可以使用命名<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
这样就保存了设置(但不是立即生效)。只是该角色再连接数据库时生效,看起来好像是开启会话前执行了命令 SET enable_indexscan TO off 一样。你也可以在会话中修改该设置,它只是默认行为。若要取消某个默认设置使用命令 ALTER ROLE rolename RESET varname. 给没有 LOGIN 权限的角色设置缺省行为是不会起作用的,因为它们从不会被调用。<br />
<br />
== 角色成员 ==<br />
It is frequently convenient to group users together to ease management of privileges: that way, privileges can be granted to, or revoked from, a group as a whole. In PostgreSQL this is done by creating a role that represents the group, and then granting membership in the group role to individual user roles.<br />
<br />
To set up a group role, first create the role:<br />
<br />
<pre>CREATE ROLE name;</pre><br />
Typically a role being used as a group would not have the LOGIN attribute, though you can set it if you wish.<br />
<br />
Once the group role exists, you can add and remove members using the GRANT and REVOKE commands:<br />
<br />
<pre>GRANT group_role TO role1, ... ;<br />
REVOKE group_role FROM role1, ... ;</pre><br />
<br />
You can grant membership to other group roles, too (since there isn't really any distinction between group roles and non-group roles). The database will not let you set up circular membership loops. Also, it is not permitted to grant membership in a role to PUBLIC.<br />
<br />
The members of a group role can use the privileges of the role in two ways. First, every member of a group can explicitly do SET ROLE to temporarily "become" the group role. In this state, the database session has access to the privileges of the group role rather than the original login role, and any database objects created are considered owned by the group role not the login role. Second, member roles that have the INHERIT attribute automatically have use of the privileges of roles of which they are members, including any privileges inherited by those roles. As an example, suppose we have done:<br />
<br />
<pre><br />
CREATE ROLE joe LOGIN INHERIT;<br />
CREATE ROLE admin NOINHERIT;<br />
CREATE ROLE wheel NOINHERIT;<br />
GRANT admin TO joe;<br />
GRANT wheel TO admin;<br />
</pre><br />
<br />
Immediately after connecting as role joe, a database session will have use of privileges granted directly to joe plus any privileges granted to admin, because joe "inherits" admin's privileges. However, privileges granted to wheel are not available, because even though joe is indirectly a member of wheel, the membership is via admin which has the NOINHERIT attribute. After:<br />
<br />
<pre>SET ROLE admin;</pre><br />
the session would have use of only those privileges granted to admin, and not those granted to joe. After:<br />
<br />
<pre>SET ROLE wheel;</pre><br />
the session would have use of only those privileges granted to wheel, and not those granted to either joe or admin. The original privilege state can be restored with any of:<br />
<br />
<pre><br />
SET ROLE joe;<br />
SET ROLE NONE;<br />
RESET ROLE;<br />
</pre><br />
<br />
Note: The SET ROLE command always allows selecting any role that the original login role is directly or indirectly a member of. Thus, in the above example, it is not necessary to become admin before becoming wheel.<br />
Note: In the SQL standard, there is a clear distinction between users and roles, and users do not automatically inherit privileges while roles do. This behavior can be obtained in PostgreSQL by giving roles being used as SQL roles the INHERIT attribute, while giving roles being used as SQL users the NOINHERIT attribute. However, PostgreSQL defaults to giving all roles the INHERIT attribute, for backward compatibility with pre-8.1 releases in which users always had use of permissions granted to groups they were members of.<br />
The role attributes LOGIN, SUPERUSER, CREATEDB, and CREATEROLE can be thought of as special privileges, but they are never inherited as ordinary privileges on database objects are. You must actually SET ROLE to a specific role having one of these attributes in order to make use of the attribute. Continuing the above example, we might choose to grant CREATEDB and CREATEROLE to the admin role. Then a session connecting as role joe would not have these privileges immediately, only after doing SET ROLE admin.<br />
<br />
To destroy a group role, use DROP ROLE:<br />
<br />
<pre>DROP ROLE name;</pre><br />
<br />
Any memberships in the group role are automatically revoked (but the member roles are not otherwise affected). Note however that any objects owned by the group role must first be dropped or reassigned to other owners; and any permissions granted to the group role must be revoked.<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203309.1第二十章2013-07-03T15:01:37Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : 角色若要创建更多的角色必须在创建该角色时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEROLE. 拥有 CREATEROLE 权限的角色可以修改和删除其他角色,同时还可以授予或撤销它们的成员属性. 但是,若要创建、修改、删除或者变更超级用户角色的成员属性需要有超级用户权限,仅仅 CREATEROLE 是不够的.<br />
<br />
;启动复制 : 角色若要启动流复制必须在创建该角色时明确给出权限。用来启动流复制的角色必须拥有 LOGIN 权限。要创建这样的角色,使用命令 CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : 口令只有在客户端试图连接数据库且客户短认证方法要求其提供口令时才有用。口令本身和 md5 认证方法可使口令生效。数据库口令和操作系统口令是独立的。创建角色并指定口令使用命令 CREATE ROLE name PASSWORD 'string'.<br />
<br />
角色属性可以使用命令 ALTER ROLE 修改。关于 CREATE ROLE 和 ALTER ROLE 命令的更多信息,请查看参考页。<br />
<br />
<blockquote>'''Tip:''' 推荐在创建一个拥有 CREATEDB 和 CREATEROLE 权限但不是超级用户的角色,并用它完成数据库和角色的日常管理工作。这种方法避免了那些本不需要超级用户权限的误操作。</blockquote><br />
<br />
可以为角色指定运行时缺省配置,这些配置在第18章有描述。例如,处于某种原因,你想让角色连接到数据库时自动关闭索引扫瞄(提示:并不是个好主意),可以使用命名<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
这样就保存了设置(但不是立即生效)。只是该角色再连接数据库时生效,看起来好像是开启会话前执行了命令 SET enable_indexscan TO off 一样。你也可以在会话中修改该设置,它只是默认行为。若要取消某个默认设置使用命令 ALTER ROLE rolename RESET varname. 给没有 LOGIN 权限的角色设置缺省行为是不会起作用的,因为它们从不会被调用。<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203299.1第二十章2013-07-03T14:45:45Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : 角色若要创建更多的角色必须在创建时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEROLE. 拥有 CREATEROLE 权限的角色可以修改和删除其他角色,同时还可以授予或撤销它们的成员属性. 但是,若要创建、修改、删除或者变更超级用户角色的成员属性需要有超级用户权限,仅仅 CREATEROLE 是不够的.<br />
<br />
;启动复制 : 角色若要启动流复制必须在创建时明确给出权限。用来启动流复制的角色必须拥有 LOGIN 权限。要创建这样的角色,使用命令 CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : 口令只有在客户端试图连接数据库且客户短认证方法要求其提供口令时才有用。口令本身和 md5 认证方法可使口令生效。数据库口令和操作系统口令是独立的。创建角色并指定口令使用命令 CREATE ROLE name PASSWORD 'string'.<br />
<br />
角色属性可以使用命令 ALTER ROLE 修改。关于 CREATE ROLE 和 ALTER ROLE 命令的更多信息,请查看参考页。<br />
<br />
<blockquote>'''Tip:''' 推荐在创建一个拥有 CREATEDB 和 CREATEROLE 权限但不是超级用户的角色,并用它完成数据库和角色的日常管理工作。这种方法避免了那些本不需要超级用户权限的误操作。</blockquote><br />
<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203289.1第二十章2013-07-03T14:26:28Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : 角色若要创建更多的角色必须在创建时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEROLE. 拥有 CREATEROLE 权限的角色可以修改和删除其他角色,同时还可以授予或撤销它们的成员属性. 但是,若要创建、修改、删除或者变更超级用户角色的成员属性需要有超级用户权限,仅仅 CREATEROLE 是不够的.<br />
<br />
;启动复制 : 角色若要启动流复制必须在创建时明确给出权限。用来启动流复制的角色必须拥有 LOGIN 权限。要创建这样的角色,使用命令 CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
<blockquote>'''Tip:''' It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.</blockquote><br />
<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203239.1第二十章2013-07-02T14:59:08Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : 角色若要创建数据库必须在创建时明确给出权限(超级用户除外,因为超级用户可以绕开所有权限检查)。要创建这样的角色,使用命令 CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
;initiating replication : A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
<blockquote>'''Tip:''' It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.</blockquote><br />
<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203229.1第二十章2013-07-02T14:52:44Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : 数据库超级用户除登陆和启动复制外,可以绕过所有权限检查。这是一个危险的权限,应当慎用,最好使用非超级用户来完成你的大部分工作。要创建数据库超级用户,使用 CREATE ROLE name SUPERUSER. 只有超级用户才可以做此操作。创建的超级用户默认授予启动流复制的权限。为了确保安全可以使用CREATE ROLE name SUPERUSER NOREPLICATION 命令禁止该权限.<br />
<br />
;创建数据库 : A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
;initiating replication : A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
<blockquote>'''Tip:''' It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.</blockquote><br />
<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203219.1第二十章2013-07-02T14:38:56Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
一个数据库角色可以有一系列属性,这些属性定义它的权限以及与客户认证系统的交互。<br />
<br />
;登陆权限 : 只有具有 LOGIN 属性的角色才可以用作数据库连接的初始角色。拥有 LOGIN 属性的角色几乎等同于“数据库用户”。要创建具有登陆权限的角色,可用下面两个命令之一。<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER 区别于 CREATE ROLE 的是 CREATE USER 默认具有登陆属性, 而 CREATE ROLE 没有.)<br />
<br />
;超级用户 : A database superuser bypasses all permission checks, except the right to log in or the right to initiate replication. This is a dangerous privilege and should not be used carelessly; it is best to do most of your work as a role that is not a superuser. To create a new database superuser, use CREATE ROLE name SUPERUSER. You must do this as a role that is already a superuser. Creating a superuser will by default also grant permissions to initiate streaming replication. For increased security this can be disallowed using CREATE ROLE name SUPERUSER NOREPLICATION.<br />
<br />
;创建数据库 : A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEDB.<br />
<br />
;创建角色 : A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
;initiating replication : A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;口令 : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
<blockquote>'''Tip:''' It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.</blockquote><br />
<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203209.1第二十章2013-07-02T14:18:20Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
A database role can have a number of attributes that define its privileges and interact with the client authentication system.<br />
<br />
;login privilege : Only roles that have the LOGIN attribute can be used as the initial role name for a database connection. A role with the LOGIN attribute can be considered the same as a "database user". To create a role with login privilege, use either:<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not.)<br />
<br />
;superuser status : A database superuser bypasses all permission checks, except the right to log in or the right to initiate replication. This is a dangerous privilege and should not be used carelessly; it is best to do most of your work as a role that is not a superuser. To create a new database superuser, use CREATE ROLE name SUPERUSER. You must do this as a role that is already a superuser. Creating a superuser will by default also grant permissions to initiate streaming replication. For increased security this can be disallowed using CREATE ROLE name SUPERUSER NOREPLICATION.<br />
<br />
;database creation : A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEDB.<br />
<br />
;role creation : A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
;initiating replication : A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;password : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
<blockquote>Tip: It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.</blockquote><br />
<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203199.1第二十章2013-07-02T14:02:15Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
A database role can have a number of attributes that define its privileges and interact with the client authentication system.<br />
<br />
;login privilege : Only roles that have the LOGIN attribute can be used as the initial role name for a database connection. A role with the LOGIN attribute can be considered the same as a "database user". To create a role with login privilege, use either:<br />
<br />
<pre><br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
</pre><br />
(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not.)<br />
<br />
;superuser status : A database superuser bypasses all permission checks, except the right to log in or the right to initiate replication. This is a dangerous privilege and should not be used carelessly; it is best to do most of your work as a role that is not a superuser. To create a new database superuser, use CREATE ROLE name SUPERUSER. You must do this as a role that is already a superuser. Creating a superuser will by default also grant permissions to initiate streaming replication. For increased security this can be disallowed using CREATE ROLE name SUPERUSER NOREPLICATION.<br />
<br />
;database creation : A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEDB.<br />
<br />
;role creation : A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
;initiating replication : A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;password : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
<pre><br />
Tip: It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.<br />
</pre><br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
<pre><br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
</pre><br />
<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203189.1第二十章2013-07-02T13:57:25Z<p>Horen: /* 角色属性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
A database role can have a number of attributes that define its privileges and interact with the client authentication system.<br />
<br />
;login privilege : Only roles that have the LOGIN attribute can be used as the initial role name for a database connection. A role with the LOGIN attribute can be considered the same as a "database user". To create a role with login privilege, use either:<br />
<br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not.)<br />
<br />
;superuser status : A database superuser bypasses all permission checks, except the right to log in or the right to initiate replication. This is a dangerous privilege and should not be used carelessly; it is best to do most of your work as a role that is not a superuser. To create a new database superuser, use CREATE ROLE name SUPERUSER. You must do this as a role that is already a superuser. Creating a superuser will by default also grant permissions to initiate streaming replication. For increased security this can be disallowed using CREATE ROLE name SUPERUSER NOREPLICATION.<br />
<br />
;database creation : A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEDB.<br />
<br />
;role creation : A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
;initiating replication : A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
;password : A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
Tip: It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203179.1第二十章2013-07-02T13:06:03Z<p>Horen: /* 角色特性 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色属性 ==<br />
A database role can have a number of attributes that define its privileges and interact with the client authentication system.<br />
<br />
login privilege<br />
Only roles that have the LOGIN attribute can be used as the initial role name for a database connection. A role with the LOGIN attribute can be considered the same as a "database user". To create a role with login privilege, use either:<br />
<br />
CREATE ROLE name LOGIN;<br />
CREATE USER name;<br />
(CREATE USER is equivalent to CREATE ROLE except that CREATE USER assumes LOGIN by default, while CREATE ROLE does not.)<br />
<br />
superuser status<br />
A database superuser bypasses all permission checks, except the right to log in or the right to initiate replication. This is a dangerous privilege and should not be used carelessly; it is best to do most of your work as a role that is not a superuser. To create a new database superuser, use CREATE ROLE name SUPERUSER. You must do this as a role that is already a superuser. Creating a superuser will by default also grant permissions to initiate streaming replication. For increased security this can be disallowed using CREATE ROLE name SUPERUSER NOREPLICATION.<br />
<br />
database creation<br />
A role must be explicitly given permission to create databases (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEDB.<br />
<br />
role creation<br />
A role must be explicitly given permission to create more roles (except for superusers, since those bypass all permission checks). To create such a role, use CREATE ROLE name CREATEROLE. A role with CREATEROLE privilege can alter and drop other roles, too, as well as grant or revoke membership in them. However, to create, alter, drop, or change membership of a superuser role, superuser status is required; CREATEROLE is insufficient for that.<br />
<br />
initiating replication<br />
A role must explicitly be given permission to initiate streaming replication. A role used for streaming replication must always have LOGIN permission as well. To create such a role, use CREATE ROLE name REPLICATION LOGIN.<br />
<br />
password<br />
A password is only significant if the client authentication method requires the user to supply a password when connecting to the database. The password and md5 authentication methods make use of passwords. Database passwords are separate from operating system passwords. Specify a password upon role creation with CREATE ROLE name PASSWORD 'string'.<br />
<br />
A role's attributes can be modified after creation with ALTER ROLE. See the reference pages for the CREATE ROLE and ALTER ROLE commands for details.<br />
<br />
Tip: It is good practice to create a role that has the CREATEDB and CREATEROLE privileges, but is not a superuser, and then use this role for all routine management of databases and roles. This approach avoids the dangers of operating as a superuser for tasks that do not really require it.<br />
A role can also have role-specific defaults for many of the run-time configuration settings described in Chapter 18. For example, if for some reason you want to disable index scans (hint: not a good idea) anytime you connect, you can use:<br />
<br />
ALTER ROLE myname SET enable_indexscan TO off;<br />
This will save the setting (but not set it immediately). In subsequent connections by this role it will appear as though SET enable_indexscan TO off had been executed just before the session started. You can still alter this setting during the session; it will only be the default. To remove a role-specific default setting, use ALTER ROLE rolename RESET varname. Note that role-specific defaults attached to roles without LOGIN privilege are fairly useless, since they will never be invoked.<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203169.1第二十章2013-07-02T13:03:09Z<p>Horen: /* 数据库角色 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色特性 ==<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=203159.1第二十章2013-07-02T13:02:29Z<p>Horen: /* 数据库角色 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
某个连接可以使用的角色集合是由��客户断认证设置决定的,这在第19章里有解释。(因此,客户端并非必须使用与操作系统同名的角色名,就像你的登陆用户名不一定是你的真实姓名一样。)因为角色身份决定了一连接客户端可用的权限,所以在多用户环境下仔细配置权限是非常重要的。<br />
<br />
== 角色特性 ==<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E7%AC%AC%E4%BA%8C%E5%8D%81%E7%AB%A0&diff=200729.1第二十章2013-06-09T09:43:36Z<p>Horen: /* 数据库角色 */</p>
<hr />
<div>'''数据库角色'''<br />
----<br />
<br />
PostgreSQL使用角色的概念来控制数据库访问权限。角色可以理解为一个数据库用户,也可以是一组用户,取决于角色的创建方式。角色拥有自己的数据库对象(比如,表),角色还可以把访问这些数据库对象的权限授予其他角色。此外,角色还可以把其成员赋给其他角色,以让该成员拥有其他角色的权限。<br />
<br />
角色是用户和组的统称。在PostgreSQL 8.1版本前,用户和组是不同的实体,但是现在只有角色的概念。任何角色都可以充当一个用户、一个组或者二者兼而有之。<br />
<br />
本章描述如何创建和管理角色。更多信息关于角色权限对数据库对象的影响,请参见第5.6章。<br />
<br />
<br />
== 数据库角色 ==<br />
数据库角色和操作系统不是同一个概念。实际使用中使二者统一可能很方便,但这不是必须的。数据库角色在数据库集群中是通用的(并不是每个数据库都不同)。创建角色使用命令 CREATE ROLE SQL :<br />
<pre><br />
CREATE ROLE name;<br />
</pre><br />
''name'' 遵循SQL命名规范:要么没有特殊字符,要么使用双引号。(实际使用中,通常给该命令加上额外选项,比如 LOGIN。更多细节如下)删除角色使用类似的命令 DROP ROLE :<br />
<pre><br />
DROP ROLE name;<br />
</pre><br />
为方便起见,程序createuser和dropuser封装了这些SQL命令,可以在shell中直接调用它们:<br />
<pre><br />
createuser name<br />
dropuser name<br />
</pre><br />
通过系统表pg_roles可以查看已存在的角色,例如<br />
<pre><br />
SELECT rolname FROM pg_roles;<br />
</pre><br />
使用psql程序的元命令''\du''也可以列出已存在的角色。<br />
<br />
为了引导数据库系统,初始化时通常包含一个预定义的角色。这个角色通常是超级用户,默认情况下(除非在运行initdb时改变)它会跟操作系统用户名相同并用此角色来初始化数据库集群。习惯上,该角色被命名为postgres。如果要创建更多的角色必须使用该初始角色连接数据库。<br />
<br />
每个数据库服务器的连接必须由某个角色创建,且该角色决定了的命令的初始访问权限。为了连接数据库,应用程序客户端需要指定角色来初始化连接请求。例如,psql程序使用-U命令行选项指定角色。很多应用程序假定角色名为当前操作系统用户名(包括''createuser''和''psql'')。因此,使角色名和操作系统用户名保持一致是很方便的。<br />
<br />
The set of database roles a given client connection can connect as is determined by the client authentication setup, as explained in Chapter 19. (Thus, a client is not limited to connect as the role matching its operating system user, just as a person's login name need not match her real name.) Since the role identity determines the set of privileges available to a connected client, it is important to carefully configure privileges when setting up a multiuser environment.<br />
<br />
== 角色特性 ==<br />
<br />
== 角色成员 ==<br />
<br />
== 函数和触发器安全 ==</div>Horenhttps://wiki.postgresql.org/index.php?title=9.1%E6%96%87%E6%A1%A3%E7%BF%BB%E8%AF%91%E9%A1%B9%E7%9B%AE&diff=200719.1文档翻译项目2013-06-09T09:09:28Z<p>Horen: </p>
<hr />
<div>参考文档<br />
*[http://www.pgsqldb.org/mwiki/index.php/PostgreSQL_8.3_%E6%96%87%E6%A1%A3 8.3 wiki版本]<br />
*[http://www.pgsqldb.org/pgsqldoc-8.1c/index.html 8.1中文文档]<br />
*[http://wiki.postgresql.org/images/7/7b/PostgreSQL8.2.3doc.zip 8.2.3中文文档]<br />
*[http://developer.postgresql.org/pgdocs/postgres/index.html 9.1文档]<br />
*[http://wiki.postgresql.org/wiki/Help:Formatting wiki语法参考]<br />
<br />
校对的时候,使用<del> </del>标记原来的内容,然后写上修改后的东西。<br />
{|border="1"<br />
|其他语言模块<br />
|模块名字<br />
|标注者<br />
|-<br />
|python<br />
|[[psycopy]]<br />
|冇得弹<br />
|-<br />
|.net<br />
|[[npgsql]]<br />
|冇得弹<br />
|-<br />
|php<br />
|直接支持<br />
|冇得弹<br />
|}<br />
翻译任务<br />
{| border=1<br />
| 部分 <br />
| 章节<br />
| 标题<br />
| 翻译志愿者<br />
| 状态<br />
| 开始时间<br />
| 计划完成时间<br />
| 备注<br />
| 文件<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 1<br />
| Getting Started<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
| [[9.1第一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Tutorial<br />
| 2<br />
| The SQL Language<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第二章]]<br />
|-<br />
| Tutorial<br />
| 3<br />
| Advanced Features<br />
| 小厨神<br />
| 基本校完<br />
|<br />
|<br />
|<br />
|[[9.1第三章]]<br />
|-<br />
| The SQL Language<br />
| 4<br />
| SQL Syntax<br />
| 冇得弹<br />
| 89%<br />
|<br />
|<br />
|<br />
|[[9.1第四章]]<br />
|-<br />
| The SQL Language<br />
| 5<br />
| Data Definition<br />
| 冇得弹<br />
| 98%<br />
|<br />
|<br />
|<br />
|[[9.1第五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| The SQL Language<br />
| 6<br />
| Data Manipulation<br />
| 冇得弹<br />
| '''完美'''<br />
| <br />
| <br />
| 这章的内容和8.1文档一样<br />
|[[9.1第六章]]<br />
|-<br />
| The SQL Language<br />
| 7<br />
| Queries<br />
| 冇得弹<br />
| <br />
| <br />
| <br />
| <br />
|[[9.1第七章]]<br />
|-<br />
| The SQL Language<br />
| 8<br />
| Data Types<br />
| 冇得弹<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第八章]]<br />
|-<br />
| The SQL Language<br />
| 9<br />
| Functions and Operators<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第九章]]<br />
|-<br />
| The SQL Language<br />
| 10<br />
| Type Conversion<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十章]]<br />
|-<br />
| The SQL Language<br />
| 11<br />
| Indexes<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十一章]]<br />
|-<br />
| The SQL Language<br />
| 12<br />
| Full Text Search<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十二章]]<br />
|-<br />
| The SQL Language<br />
| 13<br />
| Concurrency Control<br />
| 冇得弹<br />
| 开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十三章]]<br />
|-<br />
| The SQL Language<br />
| 14<br />
| Performance Tips<br />
| 冇得弹<br />
|开始中<br />
|<br />
|<br />
|<br />
|[[9.1第十四章]]<br />
|-<br />
| Server Administration<br />
| 15<br />
| Installation from Source Code<br />
| 紫轩<br />
| 基本完成<br />
|<br />
|<br />
|<br />
|[[9.1第十五章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 16<br />
| Installation from Source Code on Windows<br />
| 紫轩<br />
| 完成 校正中 (iihero补校)<br />
|<br />
|<br />
| 有些计算机专业用语不懂,恐怕会让人误解<br />
|[[9.1第十六章]]<br />
|-<br />
| Server Administration<br />
| 17<br />
| Server Setup and Operation<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十七章]]<br />
|-<br />
| Server Administration<br />
| 18<br />
| Server Configuration<br />
| 紫轩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第十八章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 19<br />
| Client Authentication<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第十九章]]<br />
|-<br />
| Server Administration<br />
| 20<br />
| Database Roles<br />
| 任洪彩<br />
| 进行中<br />
|<br />
|<br />
|<br />
|[[9.1第二十章]]<br />
|-<br />
| Server Administration<br />
| 21<br />
| Managing Databases<br />
| 任洪彩<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十一章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Administration<br />
| 22<br />
| Localization<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十二章]]<br />
|-<br />
| Server Administration<br />
| 23<br />
| Routine Database Maintenance Tasks<br />
| 小燕子 <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十三章]]<br />
|-<br />
| Server Administration<br />
| 24<br />
| Backup and Restore<br />
| 星海游侠<br />
| 完成了一半<br />
|<br />
|<br />
|<br />
|[[9.1第二十四章]]<br />
|-<br />
| Server Administration<br />
| 25<br />
| High Availability, Load Balancing, and Replication<br />
| 星海游侠<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十五章]]<br />
|-<br />
| Server Administration<br />
| 26<br />
| Recovery Configuration<br />
| 星海游侠<br />
| 完成,待校<br />
|<br />
|<br />
|<br />
|[[9.1第二十六章]]<br />
|-<br />
| Server Administration<br />
| 27<br />
| Monitoring Database Activity<br />
| 泥鳅<br />
| 完成,谁来校验下<br />
|<br />
|<br />
|<br />
|[[9.1第二十七章]]<br />
|-<br />
| Server Administration<br />
| 28<br />
| Monitoring Disk Usage<br />
| 泥鳅<br />
| 完成,很简单,不需要校验<br />
|<br />
|<br />
|<br />
|[[9.1第二十八章]]<br />
|-<br />
| Server Administration<br />
| 29<br />
| Reliability and the Write-Ahead Log<br />
| 小燕子<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第二十九章]]<br />
|-<br />
| Server Administration<br />
| 30<br />
| Regression Tests<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十章]]<br />
|-style="font-style:italic;color:green;"<br />
| Client Interfaces<br />
| 31<br />
| ibpq - C Library<br />
| 泥鳅<br />
| Working in progress<br />
| 2011-7-17<br />
| 工作量极大,不好估算时间<br />
|<br />
|[[9.1第三十一章]]<br />
|-<br />
| Client Interfaces<br />
| 32<br />
| Large Objects<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十二章]]<br />
|-<br />
| Client Interfaces<br />
| 33<br />
| ECPG - Embedded SQL in C<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十三章]]<br />
|-<br />
| Client Interfaces<br />
| 34<br />
| The Information Schema<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十四章]]<br />
|-style="font-style:italic;color:red;"<br />
| Server Programming<br />
| 35<br />
| Extending SQL<br />
| iihero<br />
| '''Start working'''<br />
|<br />
|<br />
|<br />
|[[9.1第三十五章]]<br />
|-<br />
| Server Programming<br />
| 36<br />
| Triggers<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第三十六章]]<br />
|-<br />
| Server Programming<br />
| 37<br />
| The Rule System<br />
| 小燕子<br />
| i'm here<br />
|<br />
|<br />
|<br />
|[[9.1第三十七章]]<br />
|-style="font-style:italic;color:blue;"<br />
| Server Programming<br />
| 38<br />
| Procedural Languages<br />
| 杨拼<br />
| 待校<br />
|<br />
|<br />
|<br />
|[[9.1第三十八章]]<br />
|-<br />
| Server Programming<br />
| 39<br />
| PL/pgSQL - SQL Procedural Language<br />
| 杨拼<br />
|开始了。。。<br />
|<br />
|<br />
|<br />
|[[9.1第三十九章]]<br />
|-<br />
| Server Programming<br />
| 40<br />
| PL/Tcl - Tcl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十章]]<br />
|-<br />
| Server Programming<br />
| 41<br />
| PL/Perl - Perl Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十一章]]<br />
|-<br />
| Server Programming<br />
| 42<br />
| PL/Python - Python Procedural Language<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十二章]]<br />
|-<br />
| Server Programming<br />
| 43<br />
| Server Programming Interface<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第四十三章]]<br />
|-<br />
| Reference<br />
| I<br />
| SQL Commands<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1参考1]]<br />
|-<br />
| Reference<br />
| II<br />
| PostgreSQL Client Applications<br />
| eastday<br />
| 刚刚开始。<br />
|<br />
|<br />
|大多内容和8.1相同.<br />
|[[9.1参考2]]<br />
|-<br />
| Reference<br />
| III<br />
| PostgreSQL Server Applications<br />
| <br />
|<br />
|<br />
|<br />
|<br />
|[[9.1参考3]]<br />
|- style="font-style:italic;color:red;"<br />
| Internals<br />
| 44<br />
| Overview of PostgreSQL Internals<br />
| Galy<br />
| 着手<br />
|<br />
|<br />
|<br />
| [[9.1第44章]]<br />
|-<br />
| Internals<br />
| 45<br />
| System Catalogs<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第45章]]<br />
|-<br />
| Internals<br />
| 46<br />
| Frontend/Backend Protocol<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第46章]]<br />
|-<br />
| Internals<br />
| 47<br />
| PostgreSQL Coding Conventions<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第47章]]<br />
|-<br />
| Internals<br />
| 48<br />
| Native Language Support<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第48章]]<br />
|-<br />
| Internals<br />
| 49<br />
| Writing A Procedural Language Handler<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第49章]]<br />
|-<br />
| Internals<br />
| 50<br />
| Genetic Query Optimizer<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第50章]]<br />
|-<br />
| Internals<br />
| 51<br />
| Index Access Method Interface Definition<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第51章]]<br />
|-<br />
| Internals<br />
| 52<br />
| GiST Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第52章]]<br />
|-<br />
| Internals<br />
| 53<br />
| GIN Indexes<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第53章]]<br />
|-<br />
| Internals<br />
| 54<br />
| Database Physical Storage<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第54章]]<br />
|-<br />
| Internals<br />
| 55<br />
| BKI Backend Interface<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第55章]]<br />
|-<br />
| Internals<br />
| 56<br />
| How the Planner Uses Statistics<br />
| Galy<br />
|<br />
|<br />
|<br />
|<br />
|[[9.1第56章]]<br />
|-<br />
|}<br />
<br />
志愿者简介<br />
<br />
感谢为我们的文档付出了的无私的奉献。<br />
{|border=1<br />
|网名来胳<br />
|猫<br />
|-<br />
|''泥鳅''<br />
|<br />
|-<br />
|''小神厨'' <br />
|anliulin1986 _at_ sina.com<br />
|-<br />
|''Galy''<br />
|galylee _at_ gmail.com<br />
|-<br />
|''hxre''<br />
|hxre _at_ hotmail.com<br />
|-<br />
|''冇得弹''<br />
|zelda_ok _at_ 163.com<br />
|-<br />
|星海游侠<br />
|<br />
|-<br />
|''iihero''<br />
|iihero _at_ qq.com<br />
|}<br />
<br />
[[Category:Chinese]]</div>Horen