diff --git a/src/database/driver/postgres.ts b/src/database/driver/postgres.ts index 73a1592c..cef00715 100644 --- a/src/database/driver/postgres.ts +++ b/src/database/driver/postgres.ts @@ -88,7 +88,9 @@ export async function createPostgresDatabase( * @link https://github.com/typeorm/typeorm/blob/master/src/driver/postgres/PostgresQueryRunner.ts#L326 */ let query = `CREATE DATABASE "${options.database}"`; - if (typeof options.characterSet === 'string') { + if (typeof options.template === 'string') { + query += ` TEMPLATE "${options.template}"`; + } else if (typeof options.characterSet === 'string') { query += ` WITH ENCODING '${options.characterSet}'`; } const result = await executeSimplePostgresQuery(connection, query); diff --git a/src/database/driver/type.ts b/src/database/driver/type.ts index 07fe447c..4ec842fe 100644 --- a/src/database/driver/type.ts +++ b/src/database/driver/type.ts @@ -19,7 +19,10 @@ export type DriverOptions = { characterSet?: string, // postgres specific - schema?: string + schema?: string, + + // only for postgres 13+, see https://www.postgresql.org/docs/current/manage-ag-templatedbs.html + template?: string, extra?: { [key: string]: any diff --git a/src/database/driver/utils/build.ts b/src/database/driver/utils/build.ts index 617dc989..267faf74 100644 --- a/src/database/driver/utils/build.ts +++ b/src/database/driver/utils/build.ts @@ -39,6 +39,7 @@ export function buildDriverOptions(options: DataSourceOptions): DriverOptions { ...(driverOptions.connectString ? { connectString: driverOptions.connectString } : {}), ...(driverOptions.sid ? { sid: driverOptions.sid } : {}), ...(driverOptions.serviceName ? { serviceName: driverOptions.serviceName } : {}), + ...(driverOptions.template ? { template: driverOptions.template } : {}), ...(options.extra ? { extra: options.extra } : {}), ...(driverOptions.domain ? { domain: driverOptions.domain } : {}), ...(driverOptions.schema ? { schema: driverOptions.schema } : {}),