Skip to content

psql-wrapper - simple wrapper around the psql command line utility using node's execSync

License

Notifications You must be signed in to change notification settings

paulovieira/psql-wrapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

psql-wrapper

Description

Simple wrapper around the psql command line utility using node's execSync.

Useful to make your own script runner or if you need to make a simple query to a postgres database without the complexity of the pg module (the result of the query can be saved to a .cvs file, for instance).

Install

npm install --save psql-wrapper

Example

// the module exports a function
var Psql = require('psql-wrapper.js');

// options that won't change can be set once with "configure"
Psql.configure({
    "dbname": "my_db"
});

// to execute psql (via child_process' "execSync"), call the exported function;
// any option that was previously set in "configure" will be overriden
var output = Psql({
    "command": "select c1,c2,c3 from some_table"
});
console.log(output);

// we can also save the results to a csv file using the right combination of
// options from psql
var out = Psql({
    "command": "select c1,c2,c3 from some_table",
    "output": "data.csv",
    "no-align": true,
    "tuples-only": true
});

Options for psql

Both Psql (the exported function) and Psql.configure accept an object with options to be given to the psql command line.

The object's keys should be the names of the options accepted by psql (in long format). To see them use psql --help.

The object's values are the arguments for the respective options. If the option doesn't allow an argument, the value true should be used instead.

If the value is falsy (such as an empty string), that option won't be used at all.

This module will only construct the string to be used with execSync (the shell command). It won't make any kind of validation or check to make sure the options are valid. However the psql command line is robust about these kinf of issues, so if an invalid option is given it will abort and the error message is explicit about the problem.

Extra options

The following extra options are available:

  • checkStderr: verify if stderr contains the substring 'ERROR:' and throw an error if so. This is useful to abort the execution of a script runner when some script has SQL syntax errors. Default: true.
  • psqlPath: the path to the psql command line utility. Might be necessary if you have several versions of postgres available. Default: psql.
  • displayShellCommand: display the command to be passed to execSync. Default: true.

About

psql-wrapper - simple wrapper around the psql command line utility using node's execSync

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published