简体   繁体   English

PostgreSQL - 从模板数据库创建数据库

[英]PostgreSQL - creating database from a template database

If I create a database foo that:如果我创建一个数据库foo

  1. Has initial data有初始数据
  2. Loads custom stored procedures加载自定义存储过程
  3. Loads and uses an external library加载并使用外部库

If I create a database foobar like this:如果我像这样创建一个数据库foobar

CREATE DATABASE foobar WITH ENCODING 'UTF8' TEMPLATE foo;

Will database foobar have:数据库foobar是否有:

  1. The same data that foo was initialised with foo 初始化的相同数据
  2. The same custom stored procedures loaded by foo foo加载的相同自定义存储过程
  3. The ability to use the shared library loaded in foo能够使用foo加载的共享库

[[Addendum]] [[附录]]

I have written several C library extensions which are loaded as shared libraries in the database I am using as a template.我编写了几个 C 库扩展,它们作为共享库加载到我用作模板的数据库中。 My main concern is whether these libraries will be also loaded by subsequent databases based on the template.我主要关心的是这些库是否也会被基于模板的后续数据库加载。

You need to set the database foo as a template database.您需要将数据库 foo 设置为模板数据库。

ALTER DATABASE foo IS_TEMPLATE true ; ALTER DATABASE foo IS_TEMPLATE 真;

Then you can use it to create new databases然后你可以用它来创建新的数据库

CREATE DATABASE foobar TEMPLATE foo;创建数据库 foobar 模板 foo;

why don't you try yourself?..你为什么不试试自己呢?...

t=# create database foo;
CREATE DATABASE
Time: 426.799 ms
t=# \c foo
You are now connected to database "foo" as user "postgres".

data:数据:

foo=# create table b(i int);
CREATE TABLE
Time: 3.794 ms
foo=# insert into b select 43;
INSERT 0 1
Time: 13.608 ms

fn(): fn():

foo=# create function a() returns int as $$select 42;$$ language sql;
CREATE FUNCTION
Time: 1.548 ms

extension:延期:

foo=# create extension pg_stat_statements;
CREATE EXTENSION
Time: 4.275 ms

using template:使用模板:

foo=# create database boo template foo;
CREATE DATABASE
Time: 640.328 ms
foo=# \c boo
You are now connected to database "boo" as user "postgres".

data is there:数据在那里:

boo=# select count(1) from b;
 count
-------
     1
(1 row)

Time: 1.569 ms

extension is usable:扩展可用:

boo=# select count(1) from pg_stat_statements;
 count
-------
  4927
(1 row)

Time: 1023.658 ms

fn() returns same result: fn() 返回相同的结果:

boo=# select a();
 a
----
 42
(1 row)

Time: 0.399 ms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM