最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年01月12日 10:04:02   作者:abce  
這篇文章主要介紹了PostgreSQL中的template0和template1庫(kù)使用實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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

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

刪除了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ù)。不建議用戶(hù)對(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(mén)即可。

當(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ò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • PostgreSQL之分區(qū)表(partitioning)

    PostgreSQL之分區(qū)表(partitioning)

    通過(guò)合理的設(shè)計(jì),可以將選擇一定的規(guī)則,將大表切分多個(gè)不重不漏的子表,這就是傳說(shuō)中的partitioning。比如,我們可以按時(shí)間切分,每天一張子表,比如我們可以按照某其他字段分割,總之了就是化整為零,提高查詢(xún)的效能
    2016-11-11
  • PostgreSQL忘記postgres賬號(hào)密碼的解決方法

    PostgreSQL忘記postgres賬號(hào)密碼的解決方法

    這篇文章主要介紹了PostgreSQL忘記postgres賬號(hào)的密碼的解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 如何為PostgreSQL的表自動(dòng)添加分區(qū)

    如何為PostgreSQL的表自動(dòng)添加分區(qū)

    這篇文章主要介紹了如何為PostgreSQL的表自動(dòng)添加分區(qū),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除

    postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除

    這篇文章主要介紹了postgresql如何找到表中重復(fù)數(shù)據(jù)的行并刪除問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 如何將postgresql數(shù)據(jù)庫(kù)表內(nèi)數(shù)據(jù)導(dǎo)出為excel格式(推薦)

    如何將postgresql數(shù)據(jù)庫(kù)表內(nèi)數(shù)據(jù)導(dǎo)出為excel格式(推薦)

    這篇文章主要介紹了如何將postgresql數(shù)據(jù)庫(kù)表內(nèi)數(shù)據(jù)導(dǎo)出為excel格式(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • 使用PostgreSQL創(chuàng)建高級(jí)搜索引擎的代碼示例

    使用PostgreSQL創(chuàng)建高級(jí)搜索引擎的代碼示例

    本文我們將探索PostgreSQL中的全文搜索功能,并研究我們能夠復(fù)制多少典型搜索引擎功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2023-07-07
  • PostgreSQL查看數(shù)據(jù)庫(kù)占用空間大小的幾種常用方法

    PostgreSQL查看數(shù)據(jù)庫(kù)占用空間大小的幾種常用方法

    在PostgreSQL中,查看數(shù)據(jù)庫(kù)及數(shù)據(jù)表當(dāng)前數(shù)據(jù)的占用量可以通過(guò)執(zhí)行特定的SQL查詢(xún)來(lái)實(shí)現(xiàn),本文給大家介紹了幾種常用的方法,并通過(guò)代碼示例講解的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)

    PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)

    這篇文章主要介紹了PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • postgresql查看表和索引的情況,判斷是否膨脹的操作

    postgresql查看表和索引的情況,判斷是否膨脹的操作

    這篇文章主要介紹了postgresql查看表和索引的情況,判斷是否膨脹的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Postgresql 跨庫(kù)同步表及postgres_fdw的用法說(shuō)明

    Postgresql 跨庫(kù)同步表及postgres_fdw的用法說(shuō)明

    這篇文章主要介紹了Postgresql 跨庫(kù)同步表及postgres_fdw的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01

最新評(píng)論

合山市| 夏邑县| 兴隆县| 永顺县| 东丽区| 长白| 望都县| 东光县| 石城县| 萨嘎县| 民丰县| 界首市| 尼木县| 大兴区| 广灵县| 淮滨县| 蕉岭县| 洛宁县| 大竹县| 太仓市| 贡山| 富裕县| 金秀| 颍上县| 蓬莱市| 延川县| 宿松县| 道真| 清徐县| 平果县| 临西县| 通城县| 新竹县| 桐乡市| 开封市| 伊川县| 罗山县| 昭苏县| 鹤庆县| 虎林市| 奉贤区|