Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
pg-god
Advanced tools
Readme
A tiny library that helps you create or kill PostgreSQL database.
$ 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
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.
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
// 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
}
}
FAQs
Tiny library that helps create and kill PostgreSQL database.
The npm package pg-god receives a total of 18,421 weekly downloads. As such, pg-god popularity was classified as popular.
We found that pg-god demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.