Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.20 PostgreSQL模板数据库 #276

Closed
vieyahn2017 opened this issue Apr 20, 2019 · 4 comments
Closed

4.20 PostgreSQL模板数据库 #276

vieyahn2017 opened this issue Apr 20, 2019 · 4 comments

Comments

@vieyahn2017
Copy link
Owner

PostgreSQL模板数据库

@vieyahn2017
Copy link
Owner Author

PostgreSQL基于模板创建数据库

 

基本语句:create database mydb;
 

 

该命令会以template1库为模板生成一份副本作为新database,每一个新的database都会有一个属主,它就是执行此sql命令的角色。任何一个拥有createdb权限的角色都可以创建新的database。

 

其中,template1称为数据库模板,数据库安装成功后会自动创见两个模板template0和template1。若新建数据库时未指定模板,则默认为template1,新建的数据库可以理解为template1的一份副本,其中包括所有数据库设置和数据文件。

 

切记,任何时候都不要对template0进行修改,对基于template1或自建的模板所创建的数据库来说,你不能修改其字符集编码和排序规则,如果想这么干,可以基于template0创建数据库。

@vieyahn2017
Copy link
Owner Author

基于模板创建数据库命令:

1.创建数据库

create database mydb_template;

2.将数据库设置成模板数据库

update pg_database set datistemplate = true where datname = ‘mydb_template’;

3.参照模板数据库创建新的数据库

create database mydb1 template mydb_template;

你可以以任何一个已存在的数据作为创建新数据库时的模板,此外,还可以将某个现存的数据标记为模板数据库。被标记为模板的数据库会被PostgreSQL禁止对其编辑或删除,若希望编辑或删除,将其改为普通数据库修改后再改回模板数据库。

@vieyahn2017
Copy link
Owner Author

vieyahn2017 commented Apr 20, 2019

PostgreSQL:深入理解 template1 和 template0

https://blog.csdn.net/baidu_33387365/article/details/80883142

模板数据库

模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件。
PostgreSQL安装好以后会默认附带两个模板数据库:template0和template1。
了解 PostgreSQL 的人肯定听过 template1 和 template0,这两个作为模板库,
在建库的时候会用到,但这两者是有很大差别的,曾经一段时间对这两个模板库的使
用比较模糊,今天再次查看了文档,决定通过实验记录下来。

一 关于默认模板库

1.1 默认模板库为 template1

postgres=# create database db1;
CREATE DATABASE

备注:建库时如果不指定 TEMPLATE 属性,默认用的是 template1 模板库.

1.2 手工指定模板库
postgres=# create database db2 template template0;
CREATE DATABASE
备注:也可以指定模板库为 template0

二 template1 和 template0 的区别?

数据库初始化之后, 就有了 template0, template1 库,开始时这两个库的内容是一样的,
但这两个库有啥异同呢?

任何时候都不要对template0模板数据库进行任何修改,因为这是原始的干净模板,如果其它模板数据库被搞坏了,基于这个数据库做一个副本就可以了。如果希望定制自己的模板数据库,那么请基于template1进行修改,或者自己另外创建一个模板数据库再修改。对基于template1或你自建的模板数据库创建的数据库来说,你不能修改其字符集编码和排序规则。template0可以。

2.1 template1 可以连接并创建对象,template0 不可以连接

 postgres=# \c template1
You are now connected to database “template1” as user “postgres”.

template1=# create table tmp_1( id int4);
CREATE TABLE

template1=# \c template0
FATAL:  database “template0” is not currently accepting connections
Previous connection kept

备注:当然可以通过其它方法连接 template0 库,有兴趣的同学自己研究下,这里不演示了。
正因为 template1 可以创建对像,相比 template0 ,被称为非干净数据库,而 template0
被称为干净的数据库。

2.2 使用 template1 模板库建库时不可指定新的 encoding 和 locale,而 template0 可以

 template1=# create database db3 TEMPLATE template0 ENCODING ‘SQL_ASCII’ ;
CREATE DATABASE

template1=# create database db4 TEMPLATE template1 ENCODING ‘SQL_ASCII’ ;
ERROR:  new encoding (SQL_ASCII) is incompatible with the encoding of the template database (UTF8)
HINT:  Use the same encoding as in the template database, or use template0 as template.

3 template0 库和 template1 都不可删除

 postgres=# drop database template0;
ERROR:  cannot drop a template database

postgres=# drop database template1;
ERROR:  cannot drop a template database

备注:当然有方法删除 template1 库,而且这个操作并不危险,需要修改系统表,这里不演示了。

三 关于复制数据库

之前简单介绍了 template0 和 template1 的异同,有必要介绍通过模板库复制库的操作,例如
这里已经有个 francs 库了,现在想复制一个 francs1 库,内容和 francs 库一样。

3.1 复制库

 postgres=# \c francs 
You are now connected to database “francs” as user “postgres”.

francs=# select count(*) from pg_stat_user_tables ;
 count 
——-
    41
(1 row)

postgres=# create database francs1 TEMPLATE francs ;
CREATE DATABASE

postgres=# \c francs1 francs
You are now connected to database “francs1” as user “francs”.
francs1=> select count(*) from pg_stat_user_tables ;
 count 
——-
    41
(1 row)

备注:这种方法在复制数据库时提供了方便, 也可以定制自己的数据库模板, 但是这么操作有个前提,复制时源库不可以连接,复制过程中也不允许连接源库, 否则会报以下错误:

3.2 错误代码

ERROR:  source database “francs” is being accessed by other users
DETAIL:  There is 1 other session using the database.

@vieyahn2017
Copy link
Owner Author

postgresql创建数据库

CREATE DATABASE name
[ [ WITH ] [ OWNER [=] user_name ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ LC_COLLATE [=] lc_collate ]
[ LC_CTYPE [=] lc_ctype ]
[ TABLESPACE [=] tablespace ]
[ CONNECTION LIMIT [=] connlimit ] ]

一个例子, 指定字符集

CREATE DATABASE wapreader
WITH OWNER = wapreader 
TEMPLATE = template0 
ENCODING = 'UTF8' 
TABLESPACE = tbs_wapreader;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant