embedded-postgres

17.5.0-beta.15 • Public • Published

Embedded Postgres

npm npm type definitions npm NPM GitHub Actions Workflow Status

🐘 A Node package that spawns PostgresQL clusters programatically.

Installation

embedded-postgres is available from NPM:

npm i embedded-postgres

Usage

This package contains a simple API that allows you to create clusters, start them, create / delete database and stop any existing processes.

import EmbeddedPostgres from 'embedded-postgres';

async function main() {
    // Create the object
    const pg = new EmbeddedPostgres({
        databaseDir: './data/db',
        user: 'postgres',
        password: 'password',
        port: 5432,
        persistent: true,
    });

    // Create the cluster config files
    await pg.initialise();

    // Start the server
    await pg.start();

    // Create and/or drop database
    await pg.createDatabase('TEST');
    await pg.dropDatabase('TEST');

    // Initialize a node-postgres client
    const client = pg.getPgClient();
    await client.connect();
    const result = await client.query('SELECT datname FROM pg_database');

    // Stop the server
    await pg.stop();
}

main();

PostgresQL Versions

This package aims to track the PostgresQL support policy for supported versions. Additionally, we track the binaries that are created upstream in zonky's embedded-postgres-binaries. This leads to the following current support matrix:

Platform / Architecture 12.20.0 13.16.0 14.13.0 15.8.0 16.4.0 17.0.0
🍎 Darwin / x64
🍎 Darwin / arm64[1] 🚫 🚫 🚫
🪟 Windows / x64
🐧 Linux / x64
🐧 Linux / arm
🐧 Linux / arm64
🐧 Linux / ia32
🐧 Linux / ppc64

In order to install a particular version, look for the latest tag in NPM. For example, if you would like to install v10.20.0, you can currently use the following tag:

npm i embedded-postgres@10.20.0-beta.6

Installing particular versions of PostgresQL (i.e. versions not released on NPM) is currently not possible. If you would have a need for doing so, please create an issue.


API

Options in the constructor can be used to modify the behaviour of the application. The parameters that are available as part of the options can be seen here:

Property Type Description
databaseDir string The location where the data should be persisted to. Defaults to ./data/db
port number The port where the Postgres database should be listening. Defaults to: 5432
user string The username for logging into the Postgres database. Defaults to postgres
password string The password for logging into the Postgres database. Defaults to password
authMethod 'scram-sha-256' | 'password' | 'md5' The authentication method to use when authenticating against Postgres. Defaults to password
persistent boolean Whether all data should be left in place when the database is shut down. Defaults to true.
initdbFlags string[] Pass any additional flags to the initdb process. You can find all available flags here: https://www.postgresql.org/docs/current/app-initdb.html. Flags should be passed as a string array, e.g. ["--debug"] or ["--locale=en-GB"] Defaults to []
postgresFlags string[] Pass any additional flags to the postgres process. You can find all available flags here: https://www.postgresql.org/docs/current/app-postgres.html. Flags should be passed as a string array, e.g. ["--debug"] or ["--locale=en-GB"]. Defaults to [].
createPostgresUser boolean Postgres does not allow binaries to be run by root. In case you're running in root-only enviroments, such as Docker containers, you may need to create an extra user on your system in order to be able to call the binaries.
NOTE: This WILL irreversibly modify your host system. The effects are somewhat minor, but it's still recommend to only use this in Docker containers. Defaults to false.
onLog (message | string) => void Pass in a custom logging handler. This will relay and console messages that are generated by the postgres and initdb processes. Defaults to console.log
onError (messageOrError | string | Error | unknown) => void Pass in a custom error logging handler. This will catch and stderr results coming in from the postgres and initdb processes. Defaults to console.error

Contributing

This package is open to issues, feedback, ideas and pull requests. Create an issue on this repository to get started! In order to get started with development, you might need some extra pointers

Development

In order to get yourself situated for development, you will need to the reopistory up and running. In order to make this work, start with a relatively recent install of NodeJS (at least v18, v20+ recommended). You can then run this command to install all packages:

npm install --force

[!NOTE] You must include --force or else NPM will refuse to install the dependencies for all packages, including those not for the current architecture.

Then, you must pre-compile all Typescript using the following command:

npm run build

As soon as that is complete, we'll download the requisite PostgresQL binaries for your particular architecture using:

npm run download

Lastly, you can hop over to packages/embedded-postgres and do some development there:

cd packages/embedded-postgres

You can force automatic recompliation of the Typescript files by running:

npm start

Don't forget to add and run tests when you are developing new functionality. Add them to tests/index.test.ts, and run the tests by running:

npm test

Troubleshotting

Running in Docker containers

Running in Docker containers might fail, because many are setup to run with the root user as default. Either you resolve to setting up a container with a specific user yourself, or you set the createPostgresUser option to true, after which embedded-postgres will automatically set up a postgres user on the system for usage by your script.


Credits and Licensing

Embedded Postgres was created by Lei Nelissen for BMD Studio. It is based on zonky's embedded-postgres-binaries. The binaries are made available under the Apache License 2.0, whereas the specific code in this package is made available under the MIT license.

BMD Studio

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
17.5.0-beta.15178latest

Version History

VersionDownloads (Last 7 Days)Published
17.5.0-beta.15178
16.9.0-beta.15499
15.13.0-beta.154
14.18.0-beta.154
13.21.0-beta.151
17.4.0-beta.1520
16.8.0-beta.151
15.12.0-beta.151
14.17.0-beta.151
13.20.0-beta.152
17.3.0-beta.151
16.7.0-beta.151
15.11.0-beta.154
14.16.0-beta.151
13.19.0-beta.151
17.2.0-beta.1515
16.6.0-beta.151
15.10.0-beta.1592
14.15.0-beta.151
13.18.0-beta.151
12.22.0-beta.151
17.2.0-beta.141
16.6.0-beta.141
15.10.0-beta.141
14.15.0-beta.141
13.18.0-beta.141
12.22.0-beta.141
17.1.0-beta.142
16.5.0-beta.141
15.9.0-beta.142
14.14.0-beta.141
13.17.0-beta.141
12.21.0-beta.142
17.0.0-beta.1444
16.4.0-beta.148
15.8.0-beta.141
14.13.0-beta.142
13.16.0-beta.141
12.20.0-beta.141
17.0.0-beta.131
16.4.0-beta.131
15.8.0-beta.132
14.13.0-beta.131
13.16.0-beta.131
12.20.0-beta.132
17.0.0-beta.121
16.4.0-beta.122
15.8.0-beta.121
14.13.0-beta.122
13.16.0-beta.122
12.20.0-beta.121
16.2.0-beta.12315
15.6.0-beta.121
14.11.0-beta.121
13.14.0-beta.121
12.18.0-beta.121
16.2.0-beta.116
15.6.0-beta.111
14.11.0-beta.111
13.14.0-beta.111
12.18.0-beta.112
16.1.1-beta.111
15.5.1-beta.112
14.10.1-beta.111
13.13.1-beta.111
12.17.1-beta.112
11.22.1-beta.111
16.1.1-beta.94
15.5.1-beta.963
14.10.1-beta.91
13.13.1-beta.91
12.17.1-beta.91
11.22.1-beta.92
15.4.0-beta.945
14.9.0-beta.92
13.12.0-beta.92
12.16.0-beta.91
11.21.0-beta.91
15.3.0-beta.93
14.8.0-beta.90
13.11.0-beta.91
12.15.0-beta.90
11.20.0-beta.90
15.3.0-beta.855
14.8.0-beta.80
13.11.0-beta.80
12.15.0-beta.80
11.20.0-beta.80
15.3.0-beta.70
14.8.0-beta.71
13.11.0-beta.70
12.15.0-beta.70
11.20.0-beta.70
13.10.0-beta.70
12.14.0-beta.70
11.19.0-beta.70
15.2.0-beta.60
14.7.0-beta.60
13.10.0-beta.60
12.14.0-beta.60
11.19.0-beta.61
15.1.0-beta.659
14.6.0-beta.60
13.9.0-beta.61
12.13.0-beta.60
11.18.0-beta.60
10.23.0-beta.60
15.0.0-beta.60
14.5.0-beta.60
14.4.0-beta.60
14.3.0-beta.60
13.6.0-beta.60
12.10.0-beta.60
11.15.0-beta.60
10.20.0-beta.61
14.3.0-alpha.210
14.3.0-alpha.200
14.3.0-alpha.190
14.3.0-alpha.180
14.3.0-alpha.170
14.3.0-alpha.150
14.3.0-alpha.140
14.3.0-alpha.130
14.3.0-alpha.120
14.3.0-alpha.110
14.3.0-alpha.100
14.3.0-alpha.91
14.3.0-alpha.80
14.3.0-alpha.70
14.3.0-alpha.61
14.3.0-alpha.20

Package Sidebar

Install

npm i embedded-postgres

Weekly Downloads

1,509

Version

17.5.0-beta.15

License

MIT

Unpacked Size

32.7 kB

Total Files

9

Last publish

Collaborators

  • leinelissen