万梅资源网 Design By www.ubjie.com

postgresql中默认会有三个数据库:postgres、template0、template1。

postgres=# \l
         List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =T/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
(3 rows)
 
postgres=#

客户端默认会连接到postgres库。可以删除该库,不过会影响默认客户端连接。

删除了postgres库之后,可以借助模板库template1再创建postgres库:

$ psql template1
psql (11.9)
Type "help" for help.
 
template1=# drop database postgres;
DROP DATABASE
template1=# \l
         List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
(2 rows)
 
template1=# create database postgres;
CREATE DATABASE
template1=# \l
         List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+-------------+-------------+-----------------------
 postgres | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
 template1 | postgres | UTF8  | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   +
   |   |   |    |    | postgres=CTc/postgres
(3 rows)
 
template1=#

其实,在使用create database db_name语句创建新库的时候,就是创建模板库template1的一个拷贝。

那如果我修改了template1库会怎样呢?

$ psql template1
psql (11.9)
Type "help" for help.
 
template1=# create table my_test_tab(a int);
CREATE TABLE
template1=# create extension hstore;
CREATE EXTENSION
template1=# \dx
       List of installed extensions
 Name | Version | Schema |     Description     
---------+---------+------------+--------------------------------------------------
 hstore | 1.5  | public  | data type for storing sets of (key, value) pairs
 plpgsql | 1.0  | pg_catalog | PL/pgSQL procedural language
(2 rows)
 
template1=#

修改以后,再创建新库的时候,新库也会包含上面的表和扩展:

template1=# create database db_test;
CREATE DATABASE
template1=# \c db_test
You are now connected to database "db_test" as user "postgres".
db_test=# \dx
       List of installed extensions
 Name | Version | Schema |     Description     
---------+---------+------------+--------------------------------------------------
 hstore | 1.5  | public  | data type for storing sets of (key, value) pairs
 plpgsql | 1.0  | pg_catalog | PL/pgSQL procedural language
(2 rows)
 
db_test=# \d
   List of relations
 Schema | Name  | Type | Owner 
--------+-------------+-------+----------
 public | my_test_tab | table | postgres
(1 row)
 
db_test=# 

无论,在template1中加入了什么,都会在之后新建的库中。

那template0的用途是什么呢?

db_test=# select datname,datallowconn,datistemplate from pg_database order by 3;
 datname | datallowconn | datistemplate
-----------+--------------+---------------
 postgres | t   | f
 db_test | t   | f
 template1 | t   | t
 template0 | f   | t
(4 rows)
 
db_test=#

从这里可以看到,只有template0库对应的datallowconn字段的值是F。这就是上面重建postgres的时候先登录template1而不是template0的原因。

template0是默认的不可修改的数据库。不建议用户对template0做任何修改。在初始化后的空实例中,template0和template1是完全相同的。

为什么需要两个模板库呢?假设你搞乱了template1,还可以通过template0恢复template1。

如果你想创建自己的模板库,只需将你选中库对应的datistemplate(pg_database中的列)设置为T即可。

当然,在创建新库的时候,还可以选择其他的库做为源库:

db_test=# create database db_test_2 template db_test;
CREATE DATABASE
db_test=#

但是,要求不能有其他连接连接到模板库,否则会报错:

db_test=# create database db_test_2 template db_test;
ERROR: source database "db_test" is being accessed by other users
DETAIL: There is 1 other session using the database.
db_test=#

补充:重建postgresql模板数据库template1

$ psql -U postgres postgres
postgres=# update pg_database set datistemplate = false where datname='template1';
UPDATE 1
postgres=# drop database template1;
DROP DATABASE
postgres=# create database template1 template=template0;
CREATE DATABASE
postgres=# update pg_database set datistemplate = true where datname='template1';
UPDATE 1
postgres=#

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

万梅资源网 Design By www.ubjie.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
万梅资源网 Design By www.ubjie.com

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。