Skip to content

CLI Usage

Database Connection

You can specify your database connection information using config.

jsonc
// config/default.json
{
  "db": {
    "url": "postgres://postgres:password@localhost:5432/database",
  },
}

or

jsonc
// config/default.json
{
  "db": {
    "user": "postgres",
    "password": "",
    "host": "localhost",
    "port": 5432,
    "database": "database",
  },
}

You could also specify your database url by setting the environment variable DATABASE_URL.

DATABASE_URL=postgres://postgres@localhost/database node-pg-migrate

You can specify custom JSON file with config (the format is same as for db entry of config file), for example:

jsonc
// path/to/config.json
{
  "user": "postgres",
  "password": "",
  "host": "localhost",
  "port": 5432,
  "database": "database",
}

TIP

If a .env file exists, it will be loaded using dotenv (if installed) when running the node-pg-migrate binary. If the .env file is not on the same level where the command has been called, you can use the --envPath option to point to the location of your .env file.

Depending on your project's setup, it may make sense to write some custom grunt/gulp/whatever tasks that set this env var and run your migration commands. More on that below.

Available Commands

CommandDescription
node-pg-migrate create {migration-name}creates a new migration file with a timestamp prepended to the name you provide. Dashes replace spaces and underscores.
node-pg-migrate upruns all up migrations from the current state.
node-pg-migrate up {N}runs N up migrations from the current state.
node-pg-migrate downruns a single down migration.
node-pg-migrate down {N}runs N down migrations from the current state.
node-pg-migrate redoredoes last migration (runs a single down migration, then single up migration).
node-pg-migrate redo {N}redoes N last migrations (runs N down migrations, then N up migrations).

Configuration

TIP

See all by running node-pg-migrate --help.

Most of the configuration options can be also specified in config file.

You can adjust defaults by passing arguments to node-pg-migrate:

ArgumentAliasesDefaultDescription
config-filefundefinedThe file with migration JSON config
config-valuedbName of config section with db options
schemaspublicThe schema(s) on which migration will be run, used to set search_path
create-schemafalseCreate the configured schema if it doesn't exist
database-url-vardDATABASE_URLName of env variable with database url string
migrations-dirmmigrationsThe directory containing your migration files. This path is resolved from cwd(). Alternatively, provide a glob pattern and set --use-glob. Note: enabling glob will read both, --migrations-dir and --ignore-pattern as glob patterns
use-globfalseUse glob to find migration files. This will use --migrations-dir and --ignore-pattern to glob-search for migration files.
migrations-schemasame value as schemaThe schema storing table which migrations have been run
create-migrations-schemafalseCreate the configured migrations schema if it doesn't exist
migrations-tabletpgmigrationsThe table storing which migrations have been run
ignore-patternundefinedRegex pattern for file names to ignore (ignores files starting with . by default). Alternatively, provide a glob pattern and set --use-glob. Note: enabling glob will read both, --migrations-dir and --ignore-pattern as glob patterns
migration-filename-formattimestampChoose prefix of file, utc (20200605075829074) or timestamp (1591343909074)
migration-file-languagejjsLanguage of the migration file to create (js, ts or sql)
template-file-nameundefinedUtilize a custom migration template file with language inferred from its extension. The file should export the up method, accepting a MigrationBuilder instance.
tsconfigundefinedPath to tsconfig.json. Used to setup transpiling of TS migration files. (Also sets migration-file-language to typescript, if not overridden)
envPathsame level where it's invokedRetrieve the path to a .env file. This feature proves handy when dealing with nested projects or when referencing a global .env file.
timestampfalseTreats number argument to up/down migration as timestamp (running up migrations less or equal to timestamp or down migrations greater or equal to timestamp)
check-ordertrueCheck order of migrations before running them, to switch it off supply --no-check-order
single-transactiontrueCombines all pending migrations into a single transaction so that if any migration fails, all will be rolled back, to switch it off supply --no-single-transaction
no-lockfalseDisables locking mechanism and checks
fakefalseMark migrations as run without actually performing them, (use with caution!)
decamelizefalseRuns decamelize on table/column/etc. names
verbosetruePrint all debug messages like DB queries run, to switch it off supply --no-verbose
reject-unauthorizedundefinedSets ssl rejectUnauthorized parameter. Use for e.g. self-signed certificates on the server. see

For SSL connection to DB you can set PGSSLMODE environment variable to value from list other than disable. e.g. PGSSLMODE=require node-pg-migrate up (pg will take it into account)

JSON Configuration

TIP

You can use config or your own json file with configuration (config-file command line option).

You can also specify your database connection here, see.

Other available options are:

jsonc
{
  "schema": "public",
  "createSchema": false,
  "migrationsDir": "migrations",
  "migrationsSchema": "public",
  "createMigrationsSchema": false,
  "migrationsTable": "pgmigrations",
  "migrationFilenameFormat": "utc",
  "migrationFileLanguage": "js",
  "ignorePattern": undefined,
  "checkOrder": true,
  "verbose": true,
  "decamelize": false,
  "tsconfig": "tsconfig.json",
}