Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the template database collection coding

I want build new postgreSQL database by:

CREATE DATABASE newdb WITH OWNER = postgres    ENCODING = 'UTF8'    TABLESPACE = pg_default    LC_COLLATE = 'zh_CN.UTF-8'    CONNECTION LIMIT = -1; 

and the error is:

ERROR: new collation (zh_CN.UTF-8) is incompatible with the collation of the template database (en_US.UTF8)
HINT: Use the same collation as in the template database, or use template0 as template.

How to change the template database collection?

like image 817
user504909 Avatar asked Sep 18 '13 11:09

user504909


People also ask

What is a database template?

A database template (. accdt) is a file you can use the create a new database. An application part template (. accdt) are like database templates, however, you can also use them to add elements to an existing database.

What is template0 and template1 in Postgres?

There is a second standard system database named template0 . This database contains the same data as the initial contents of template1 , that is, only the standard objects predefined by your version of PostgreSQL. template0 should never be changed after the database cluster has been initialized.


1 Answers

From PostgreSQL documentation:

Another 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. This is because template1 might contain encoding-specific or locale-specific data, while template0 is known not to.

You can use only template0 to create new database with different encoding and locale:

CREATE DATABASE newdb WITH OWNER = postgres    ENCODING = 'UTF8'    TABLESPACE = pg_default    LC_COLLATE = 'zh_CN.UTF-8'    CONNECTION LIMIT = -1    TEMPLATE template0; 

This will work, however it means that any changes you made to template1 won't be applied to newly created database.

To change encoding and collation of template1 you have to first delete template1 and then create new template template1 from template0. How to drop template database is described here. Then you can create new database template1 with chosen encoding/collation and mark it as a template by setting datistemplate=true (example):

update pg_database set datistemplate=true  where datname='template1'; 
like image 70
Tomas Greif Avatar answered Oct 07 '22 22:10

Tomas Greif