Postgresql create database from backup programmaticly I got a big problems here. Let's say I got the table A (with columns like id, name etc) in the main database. So when I making INSERT into A  I need to create new database lets say B_id. I got .backup file with database sturcture which suppose to use B_id databases.  

I guess I need to write trigger which activates script.

But how I suppose to wrtie the script which creates database(with name depends on id number) and applies .backup file to it? Is it even okay to do like that?
Creating a database inside a transaction is not possible, so that precludes doing it directly in a trigger, and more generally in any function called by the SQL engine.

The usual method to issue script-like operations from a trigger is to have such script running as a daemon and listening to events through the SQL LISTEN command, the events being signalled from the trigger with a NOTIFY command, possibly including a payload with parameters. If the transaction is rolled back, its notifications are not sent.

From a design point of view, this is a misuse of triggers. A trigger that "does something" is a side effect by nature and side effects are more a problem than a solution in programming. Creating and populating a new database is a massive side-effect.

Unfortunately some think that triggers being cool, they should use and abuse them no matter what problem they're trying to solve.