Start and stop a local PostgreSQL server in Node.js like a boss.
The constructor exported by this module accepts a single argument; a string that is a path to PostgreSQL database files or an object for configuration.
const Postgres = require('postgres');
// Simply pass a path to database files you want a PostgreSQL server to use.
const server = new Postgres('path/to/datadir');
server.open((err) => {
if (err === null) {
// You may now connect a client to the PostgreSQL server.
}
});
Property | Type | Default | Description |
---|---|---|---|
bin | String | postgres | A path to a PostgreSQL server binary. |
conf | String | A path to a PostgreSQL configuration file. | |
datadir | String | A path to PostgreSQL server files. | |
port | Number | 5432 | A port to bind a PostgreSQL server to. |
shutdown | String | fast | A PostgreSQL server shutdown mode or process signal. |
A PostgreSQL server binary must be available. If you do not have one in $PATH, provide a path in configuration.
const server = new Postgres({
port: 5432,
bin: '/opt/local/bin/postgres'
});
You may use a PostgreSQL configuration file instead of configuration object
properties that are flags (i.e. port
and datadir
). If conf
is
provided, no other flags will be passed to the binary.
const server = new Postgres({
conf: '/path/to/postgresql.conf'
});
For methods that accept callback
, callback
will receive an Error
as the first argument if a problem is detected; null
, if not.
Attempt to open a PostgreSQL server. Returns a Promise
.
server.open().then(() => {
// You may now connect to the PostgreSQL server.
});
server.open((err) => {
if (err === null) {
// You may now connect to the PostgreSQL server.
}
});
Close the associated PostgreSQL server. Returns a Promise
. NOTE: Disconnect
clients prior to calling this method to avoid receiving connection
errors from clients.
server.close().then(() => {
// The PostgreSQL server is now closed.
});
server.close((err) => {
// The PostgreSQL server is now closed.
});
Determine if the instance is starting a PostgreSQL server; true
while a
process is spawning, and/or about to be spawned, until the contained PostgreSQL
server either starts or errs.
Determine if the instance is running a PostgreSQL server; true
once a process
has spawned and the contained PostgreSQL server is ready to service requests.
Determine if the instance is closing a PostgreSQL server; true
while a
process is being, or about to be, killed until the contained PostgreSQL server
either closes or errs.
Emitted when a PostgreSQL server prints to stdout or stderr.
Emitted when attempting to start a PostgreSQL server.
Emitted when a PostgreSQL server becomes ready to service requests.
Emitted when attempting to stop a PostgreSQL server.
Emitted once a PostgreSQL server has stopped.