Skip to content

Instantly share code, notes, and snippets.

@isheraz
Last active December 13, 2019 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isheraz/9a02e937852196e0d8a701d40f00cfae to your computer and use it in GitHub Desktop.
Save isheraz/9a02e937852196e0d8a701d40f00cfae to your computer and use it in GitHub Desktop.
Postgres backup restore without password
//To Restore The Database Without Password command terminal
// psql --dbname=postgresql://USER:123@localhost:5432/DATABASENAME < PATHTOSTOREFILEAT.sql
//To Generate a Dump of The Database Without Password command terminal
// pg_dump --dbname=postgresql://USER:PASSWORD@localhost:5432/DATABASENAME > PATHTOSTOREFILEAT.sql
// Using NodeJS Process
"use strict";
const execFile = require('child_process').execFile;
const fs = require('fs');
module.exports = async (fileName) => {
const command = "pg_dump";
const args = ['--dbname=postgresql://' + process.env.DB_USER +
':' + process.env.DB_PASS + '@' + process.env.DB_HOST + ':' + process.env.DB_PORT + '/' + process.env.DB_NAME];
const basePath = process.env.DATABASE_BACKUP_STORE;
let filePathName = basePath + "/" + fileName;
execFile(command, args, {maxBuffer: (3000 * 3000)}, (error, stdout, stderr) => {
if (error) {
console.error('stderr', stderr);
throw error;
}
let writeBuffer = new Buffer(stdout);
fs.writeFile(filePathName, writeBuffer, (err) => {
if (err) console.log(err);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment