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庫之后,可以借助模板庫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=#
如果你想創建自己的模板庫,只需將你選中庫對應的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=#