How to Use a Custom Template Database in PostgreSQL

Published: 30 May 2019
on channel: PG Casts by Hashrocket
1,977
12

This PG Casts episode is sponsored by Hashrocket, a consultancy specializing in PostgreSQL; learn more at https://hashrocket.com. To see more PG Casts videos, visit our YouTube channel or https://www.pgcasts.com

Transcript:

Hey everyone, today we're going to look at how to create and use a custom template database in Postgres.

To create a new template database, we use the normal "create database" command, passing the parameter "is_template" with a value of "true".

If we are customizing locales or encoding on our new custom template, we should make sure that we are cloning from Postgres's `template0`, rather than the default `template1`.

```sql
create database example is_template true encoding 'SQL_ASCII' template template0;
```

We can see our new template database with the list command.

```
\l
```

And we can verify that it is a template by checking out the pg_database data.

```sql
select datistemplate from pg_database where datname = 'example';
```

To create a new database cloned from our custom template, we use the "create database" command, passing our template name to the template parameter.

```sql
create database test template example;
```

Using the list metacommand again, we can see that our new database has the SQL_ASCII encoding from our custom template.

```
\l
```

Thanks for watching!


Watch video How to Use a Custom Template Database in PostgreSQL online without registration, duration hours minute second in high quality. This video was added by user PG Casts by Hashrocket 30 May 2019, don't forget to share it with your friends and acquaintances, it has been viewed on our site 1,977 once and liked it 12 people.