校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > PostgreSQL中的template0和template1庫使用實戰

PostgreSQL中的template0和template1庫使用實戰

熱門標簽:移動外呼系統模擬題 江蘇400電話辦理官方 廣州電銷機器人公司招聘 濟南外呼網絡電話線路 地圖標注要花多少錢 天津開發區地圖標注app 電銷機器人能補救房產中介嗎 400電話申請客服 電話機器人怎么換人工座席

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=#

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • PostgreSQL存儲過程用法實戰詳解
  • PostgreSQL實戰之啟動恢復讀取checkpoint記錄失敗的條件詳解
  • postgresql影子用戶實踐場景分析

標簽:濮陽 寶雞 昭通 杭州 榆林 溫州 海西 辛集

巨人網絡通訊聲明:本文標題《PostgreSQL中的template0和template1庫使用實戰》,本文關鍵詞  PostgreSQL,中的,template0,和,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PostgreSQL中的template0和template1庫使用實戰》相關的同類信息!
  • 本頁收集關于PostgreSQL中的template0和template1庫使用實戰的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 通化市| 红桥区| 西吉县| 应城市| 静安区| 鸡西市| 富宁县| 临沭县| 安陆市| 通州区| 诏安县| 南汇区| 连山| 平泉县| 日照市| 乌拉特前旗| 平陆县| 洪泽县| 舞阳县| 余姚市| 赫章县| 大邑县| 正蓝旗| 佛冈县| 北京市| 安阳市| 新宁县| 岐山县| 眉山市| 逊克县| 城口县| 丹巴县| 岢岚县| 焦作市| 平山县| 焦作市| 独山县| 武义县| 五峰| 麦盖提县| 宝山区|