濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > PostgreSQL中的template0和template1庫(kù)使用實(shí)戰(zhàn)

PostgreSQL中的template0和template1庫(kù)使用實(shí)戰(zhàn)

熱門標(biāo)簽:移動(dòng)外呼系統(tǒng)模擬題 江蘇400電話辦理官方 廣州電銷機(jī)器人公司招聘 濟(jì)南外呼網(wǎng)絡(luò)電話線路 地圖標(biāo)注要花多少錢 天津開發(fā)區(qū)地圖標(biāo)注app 電銷機(jī)器人能補(bǔ)救房產(chǎn)中介嗎 400電話申請(qǐng)客服 電話機(jī)器人怎么換人工座席

postgresql中默認(rèn)會(huì)有三個(gè)數(shù)據(jù)庫(kù):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=#

客戶端默認(rèn)會(huì)連接到postgres庫(kù)。可以刪除該庫(kù),不過(guò)會(huì)影響默認(rèn)客戶端連接。

刪除了postgres庫(kù)之后,可以借助模板庫(kù)template1再創(chuàng)建postgres庫(kù):

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

其實(shí),在使用create database db_name語(yǔ)句創(chuàng)建新庫(kù)的時(shí)候,就是創(chuàng)建模板庫(kù)template1的一個(gè)拷貝。

那如果我修改了template1庫(kù)會(huì)怎樣呢?

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

修改以后,再創(chuàng)建新庫(kù)的時(shí)候,新庫(kù)也會(huì)包含上面的表和擴(kuò)展:

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

無(wú)論,在template1中加入了什么,都會(huì)在之后新建的庫(kù)中。

那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庫(kù)對(duì)應(yīng)的datallowconn字段的值是F。這就是上面重建postgres的時(shí)候先登錄template1而不是template0的原因。

template0是默認(rèn)的不可修改的數(shù)據(jù)庫(kù)。不建議用戶對(duì)template0做任何修改。在初始化后的空實(shí)例中,template0和template1是完全相同的。

為什么需要兩個(gè)模板庫(kù)呢?假設(shè)你搞亂了template1,還可以通過(guò)template0恢復(fù)template1。

如果你想創(chuàng)建自己的模板庫(kù),只需將你選中庫(kù)對(duì)應(yīng)的datistemplate(pg_database中的列)設(shè)置為T即可。

當(dāng)然,在創(chuàng)建新庫(kù)的時(shí)候,還可以選擇其他的庫(kù)做為源庫(kù):

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

但是,要求不能有其他連接連接到模板庫(kù),否則會(huì)報(bào)錯(cuò):

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

補(bǔ)充:重建postgresql模板數(shù)據(jù)庫(kù)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=#

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • PostgreSQL存儲(chǔ)過(guò)程用法實(shí)戰(zhàn)詳解
  • PostgreSQL實(shí)戰(zhàn)之啟動(dòng)恢復(fù)讀取checkpoint記錄失敗的條件詳解
  • postgresql影子用戶實(shí)踐場(chǎng)景分析

標(biāo)簽:濮陽(yáng) 寶雞 昭通 杭州 榆林 溫州 海西 辛集

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PostgreSQL中的template0和template1庫(kù)使用實(shí)戰(zhàn)》,本文關(guān)鍵詞  PostgreSQL,中的,template0,和,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PostgreSQL中的template0和template1庫(kù)使用實(shí)戰(zhàn)》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于PostgreSQL中的template0和template1庫(kù)使用實(shí)戰(zhàn)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    得荣县| 昌吉市| 华亭县| 石柱| 额济纳旗| 营山县| 永胜县| 南安市| 东至县| 石楼县| 普安县| 胶州市| 涞水县| 榆中县| 南涧| 德兴市| 中阳县| 巴东县| 林芝县| 博湖县| 丰城市| 临澧县| 巴塘县| 开封市| 乐清市| 文昌市| 买车| 蕲春县| 永修县| 岱山县| 济阳县| 章丘市| 亚东县| 英德市| 长汀县| 西盟| 石棉县| 甘泉县| 长阳| 景谷| 昌江|