1

I have a requirement where I have to create a new database in PostgreSQL server for every user that signs in. Hence 1 database per user. Also, that database will have few custom defined functions that will be called. I want to automate this process of creating the custom defined functions as soon as I create a database. So that I don't have to explicitly go and execute the function in each database.

Is this possible?

3
  • 1
    Is it possible? Yes, create the functions in the template1 database. Does it make sense? Most probably not. Commented Apr 18, 2017 at 19:30
  • 1
    as @a_horse_with_no_name said, you can create all needed relations in sample database and then "clone" it with create database new_db with template sample_db; Commented Apr 18, 2017 at 19:33
  • Although a template database is a good solution, I would prefer something like Liqubase or Flyway to manage the whole database setup script (including tables and functions). That script should be stored in a version control system like git or Subversion to manage those scripts (to prevent something like this) Commented Apr 18, 2017 at 19:58

1 Answer 1

1

From docs:

CREATE DATABASE actually works by copying an existing database. By default, it copies the standard system database named template1. Thus that database is the "template" from which new databases are made. If you add objects to template1, these objects will be copied into subsequently created user databases. This behavior allows site-local modifications to the standard set of objects in databases. 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.

So you can create "default" objects in template1 db and "clone" it. Or you can create all needed relations (tables, functions, etc) in sample database and then "clone" it with

create database new_db with template sample_db;

Same way you can create several template databases with different "default" pre-created objects and just "clone" them as needed.

Read docs above about limitations etc.

Update Adding Postgres Objects to Template1 will show you how to do it with pgadmin

Sign up to request clarification or add additional context in comments.

3 Comments

You don't need a special sample database for that. It's perfectly fine to create those functions in template1
yes, but I thought, next OP's question would be how to regenerate template1 database to "default", so I thought coming this step and showing how to create a database with template.
How do I create a function inside the template1 database using pgAdmin client? I do not see any way I can load the template1 database, however, I do see the postgres database . Is there a statement, which i can load the context of template1 db, and whatever functions i create, get created under the template1 db then. Like in SQL we had the 'using <database name>' command

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.