![]() The principal limitation is that no other sessions can be connected to the source database while it is being copied. It is important to understand, however, that this is not (yet) intended as a general-purpose “ COPY DATABASE” facility. It is possible to create additional template databases, and indeed one can copy any database in a cluster by specifying its name as the template for CREATE DATABASE. To create a database by copying template0, use:ĬREATE DATABASE dbname TEMPLATE template0 This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to. ![]() This is particularly handy when restoring a pg_dump dump: the dump script should be restored in a pristine database to ensure that one recreates the correct contents of the dumped database, without conflicting with objects that might have been added to template1 later on.Īnother common reason for copying template0 instead of template1 is that new encoding and locale settings can be specified when copying template0, whereas a copy of template1 must use the same settings it does. By instructing CREATE DATABASE to copy template0 instead of template1, you can create a “ pristine” user database (one where no user-defined objects exist and where the system objects have not been altered) that contains none of the site-local additions in template1. template0 should never be changed after the database cluster has been initialized. ![]() This database contains the same data as the initial contents of template1, that is, only the standard objects predefined by your version of PostgreSQL. There is a second standard system database named template0. For example, if you install the procedural language PL/Perl in template1, it will automatically be available in user databases without any extra action being taken when those databases are created. This behavior allows site-local modifications to the standard set of objects in databases. If you add objects to template1, these objects will be copied into subsequently created user databases. Thus that database is the “ template” from which new databases are made. By default, it copies the standard system database named template1. As such, this template database should never be modified in any way as soon as the database cluster has been initialized.CREATE DATABASE actually works by copying an existing database. We could think of this template database as a fallback if anything irreversible happens to template1. Template0 contains the same data as template1. a production database), it would be more ideal to use the Postgres utility pg_dump. As such, if you are looking to replicate a database while maintaining your connections (eg.CREATE DATABASE immediately fails if any connections exist at the start of the query. To properly create a database from a custom template database, there should be no other connections present.If you wish, you can create multiple template databases for various use cases.You can safely drop the entire custom template database without the risk of breaking CREATE DATABASE.With this, you can now have customized templates without the need to worry about polluting template1.CREATE DATABASE new_db_name TEMPLATE template_db_name Advantages
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |