npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

pg-god

v1.0.12

Published

Tiny library that helps create and kill PostgreSQL database.

Downloads

186,404

Readme

pg-god 😇

A tiny library that helps you create or kill PostgreSQL database.

oclif Version Downloads/week License

Usage

$ npm install -g pg-god

Use as CLI to create/drop database. See full API at cli-commands.

$ pg-god db-create --databaseName=pokemon-bank
$ pg-god db-drop --databaseName=pokemon-bank

Programmatic invocation

import { createDatabase, dropDatabase } from 'pg-god'

async function main() {
  await createDatabase({ databaseName: 'pokemon-bank' })
  await dropDatabase({ databaseName: 'pokemon-bank' })
}

API

function createDatabase(newDbConfig: NewDbConfig, dbCredential?: Partial<DbCredential>): Promise<void>

function dropDatabase(dropDbConfig: DropDbConfig, dbCredential?: Partial<DbCredential>): Promise<void>

export type NewDbConfig = {
  databaseName: string,
  errorIfExist?: boolean,
}

export type DropDbConfig = {
  databaseName: string,
  errorIfNonExist?: boolean,
  dropConnections?: boolean,
}

export type DbCredential = {
  user: string
  database: string
  port: number
  host: string
  password: string
}

const defaultDbCred: DbCredential = {
  user: 'postgres',
  database: 'postgres',
  password: '',
  port: 5432,
  host: 'localhost',
}

You may also use this to power TypeORM, see details at With TypeORM.

CLI Commands

pg-god db-create

create an empty database

USAGE
  $ pg-god db-create

OPTIONS
  -e, --errorIfExist               [default: false] whether throw error if DB already exists
  -h, --help                       show CLI help
  -o, --host=host                  [default: localhost] DB host
  -i, --initialDb=initialDb        [default: postgres] Initial DB name
  -n, --databaseName=databaseName  new DB name
  -l, --url=url                    new DB URL
  -p, --port=port                  [default: 5432] DB port, default `5432`
  -u, --userName=userName          [default: postgres] DB user name
  -w, --password=password          [default: empty] DB password

ALIASES
  $ pg-god db:create

ALTERNATIVE_ENV
  DB_ERROR_IF_EXIST=errorIfExist
  DB_INITIAL=initialDb
  DB_NAME=databaseName
  DB_USERNAME=userName
  DB_PORT=port
  DB_HOST=host
  DB_PASSWORD=password
  DB_URL=url

EXAMPLES
  $ pg-god db-create --databaseName=bank-db
  $ DB_NAME=bank-db pg-god db-create
  $ pg-god db-create --url postgresql://localhost:5432/bank-db
  $ pg-god db-create --databaseName=bank-db --errorIfExist
  $ pg-god db-create --databaseName=bank-db --password=123 --port=5433 --host=a.example.com --userName=beer

pg-god db-drop

drop a database

USAGE
  $ pg-god db-drop

OPTIONS
  -e, --errorIfNonExist            [default: false] whether throw error if DB doesn't exist
  -d, --dropConnections            [default: true] whether automatically drop DB connections
  -h, --help                       show CLI help
  -o, --host=host                  [default: localhost] DB host
  -i, --initialDb=initialDb        [default: postgres] Initial DB name
  -n, --databaseName=databaseName  name of DB that will be dropped
  -l, --url=url                    URL of DB that will be dropped
  -p, --port=port                  [default: 5432] DB port, default `5432`
  -u, --userName=userName          [default: postgres] DB user name
  -w, --password=password          [default: empty] DB password

ALIASES
  $ pg-god db:drop

ALTERNATIVES
  DB_ERROR_IF_NON_EXIST=errorIfNonExist
  DROP_CONNECTIONS=dropConnections
  DB_INITIAL=initialDb
  DB_NAME=databaseName
  DB_USERNAME=userName
  DB_PORT=port
  DB_HOST=host
  DB_PASSWORD=password
  DB_URL=url

EXAMPLES
  $ pg-god db-drop --databaseName=bank-db
  $ DB_NAME=bank-db pg-god db-drop
  $ pg-god db-drop --url postgresql://localhost:5432/bank-db
  $ pg-god db-drop --databaseName=bank-db --errorIfNonExist --no-dropConnections
  $ pg-god db-drop --databaseName=bank-db --password=123 --port=5433 --host=a.example.com --userName=beer

pg-god help [COMMAND]

display help for pg-god

USAGE
  $ pg-god help [COMMAND]

ARGUMENTS
  COMMAND  command to show help for

OPTIONS
  --all  see all commands in CLI

See code: @oclif/plugin-help

With TypeORM

// at index.ts
import { createDatabase } from 'pg-god'
import { createConnection, Connection, getConnection, getConnectionOptions } from 'typeorm'

let conn: Connection | undefined

export async function superCreateConnection(): Promise<Connection> {
  if (conn) return conn
  // may either read from ormconfig or hardcode your options here
  const ormOpts: PostgresConnectionOptions = await getConnectionOptions()
  try {
    conn = await createConnection(ormOpts)
    return conn
  } catch (error) {
    if (error.code === '3D000') {
      // Database doesn't exist.
      // PG error code ref: https://docstore.mik.ua/manuals/sql/postgresql-8.2.6/errcodes-appendix.html
      await createDatabase(
        { databaseName: ormOpts.database },
        {
          user: ormOpts.username,
          port: ormOpts.port,
          host: ormOpts.host,
          password:
            (typeof ormOpts.password === 'undefined') ? undefined :
            (typeof ormOpts.password === 'string') ? ormOpts.password :
            await ormOpts.password()
          ,
        }
      )
      return superCreateConnection()
    }
    throw error
  }
}