Backup does not store event triggers or extensions

0

I have a database PostgreSQL (version 9.5), which I need to make a backup, without storing the data of the tables.

When doing the backup (from pgAdmin) I select pre-data and post-data .

The problem I'm finding is that by making restore in a new database empty, the tables and triggers are created correctly, but there is no trace of the EVENT TRIGGERS .

I have also noticed that the extensions are not added either, so before doing the restore I have to add them to omit errors:

create extension postgis;
create extension citext;

Is there any option at the time of making the backup for postgres to write the necessary instructions to add the event triggers and extensions in the backup file?

    
asked by Jose Hermosilla Rodrigo 22.04.2017 в 20:52
source

1 answer

0

In case it is useful to some future reader, I have not currently found any way for the .backup file that PostgreSQL generates to include the statements to create extensiones and event-triggers .

My solution is to make a script (with NodeJS, which is what I am working with in my application), to do the whole process of creating the database, step by step, in the order in which they should be created. :

import { exec as exec_ } from 'child_process';
import { dbConfig } from '../../config';

const exec = require('bluebird').promisify(exec_);
let execOpts = { env : { 'PGPASSWORD' : dbConfig.password } };

async function existDatabase(){
    console.log('Comprobando si existe la base de datos ${dbConfig.database}');
    let commandExistDb = 'psql -U ${dbConfig.user} -tc "SELECT 1 FROM pg_database WHERE datname = '${dbConfig.database}'"'
    let promise = exec(commandExistDb, execOpts);
    return +await promise;
}

async function createDatabase(){
    console.log('Creando la base de datos ${dbConfig.database}')
    let commandCreate = 'psql -U ${dbConfig.user} -c "CREATE DATABASE ""${dbConfig.database}"" "';
    let promise = exec(commandCreate, execOpts);
    return await promise;
}

async function createExtensions() {
    console.log('Creando las extensiones postgis y citext');
    let command = 'psql -U ${dbConfig.user} -d ${dbConfig.database} -f create-extensions.sql';
    let promise = exec(command, execOpts);
    return await promise;
}

async function createTables() {
    console.log('Creando las tablas del schema public');
    let command = 'pg_restore --host localhost --port 5432 --username "${dbConfig.user}" --dbname "${dbConfig.database}" --section pre-data --section post-data --schema public "db-only.backup"';
    let promise = exec(command, execOpts);
    return await promise;
}

async function createCapasSchema() {
    console.log('Creando el schema capas')
    let commandCreate = 'psql -U ${dbConfig.user} -d ${dbConfig.database} -c "CREATE SCHEMA IF NOT EXISTS capas"';
    let promise = exec(commandCreate, execOpts);
    return await promise;
}

async function createEventTriggers() {
    console.log('Creando EVENT-TRIGGERS');
    let command = 'psql -U ${dbConfig.user} -d ${dbConfig.database} -f create-event-triggers.sql';
    let promise = exec(command, execOpts);
    return await promise;
}

async function addCapas() {
    console.log('Creando capas');
    let command = 'pg_restore -U postgres --host localhost --port 5432 --username "${dbConfig.user}" --dbname "${dbConfig.database}" --section pre-data --section data --schema capas "capas.backup"';
    let promise = exec(command, { execOpts }).catch(err => console.log(err));
    return await promise;
}

async function addDatos(){
    console.log('Añadiendo datos')
    let commandDisableTrigger = 'psql -U ${dbConfig.user} -d "${dbConfig.database}" -c "ALTER TABLE datos DISABLE TRIGGER trg_check_gid_layer_exists"';
    let commandEnableTrigger = 'psql -U ${dbConfig.user} -d "${dbConfig.database}" -c "ALTER TABLE datos ENABLE TRIGGER trg_check_gid_layer_exists"';
    let commandAdd = 'pg_restore --host localhost --port 5432 --username "${dbConfig.user}" --dbname "${dbConfig.database}" --section data --table datos --schema public --verbose "tabla-datos.backup"';
    await exec(commandDisableTrigger, execOpts);
    await exec(commandAdd);
    await exec(commandEnableTrigger, execOpts);
}

async function create(){
    let exist = await existDatabase();
    if(exist !== 1){
        await createDatabase();
        await createExtensions();
        await createTables();
        await createCapasSchema();
        await createEventTriggers();
        await addCapas();
        await addDatos();
    }
}

create();
    
answered by 26.04.2017 в 15:19