PostgreSQL creates a database based on a template

PostgreSQL creates database based on template

 

 

Basic statement: create database mydb;

 

 

This command will use the template1 library as a template to generate a copy as a new database , each new database will have an owner, which is the role of executing this sql command. Any role with createdb permission can create a new database .

 

Among them, template1 is called database template. After the database is installed successfully, two templates template0 and template1 will be automatically created by Transcend . If no template is specified when creating a new database, the default is template1 . The new database can be understood as a copy of template1 , including all database settings and data files.

 

Remember, do not modify template0 at any time. For the database created based on template1 or a self-built template, you cannot modify its character set encoding and collation. If you want to do this, you can create a database based on template0 .

 

Create database command based on template:

 

1. Create a database

create database mydb_template;

 

2. Set the database to a template database

update pg_database set datistemplate = true where datname = mydb_template;

 

3. Create a new database with reference to the template database

create database mydb1 template mydb_template;

 

You can use any existing data as a template when creating a new database. In addition, you can mark an existing data as a template database. The database marked as a template will be prohibited from being edited or deleted by PostgreSQL . If you want to edit or delete it, change it to a normal database and then change it back to the template database.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326382561&siteId=291194637