Skip to content

Connecting to Database

Faisal Ali edited this page Feb 23, 2020 · 5 revisions

Introduction

There are three ways to connect to database using this tool

  1. Using the command line arguments
  2. Using the environment variables
  3. Using the default arguments

NOTE: When using the above option, the precedence is taken based on the order mentioned above, i.e command line take precedence over environment variable, environment variable over default etc.

Command line arguments

The command line argument flags available to connect to database includes

  -a, --address string    Hostname where the postgres database lives
  -d, --database string   Database to mock the data (default "gpadmin")
  -w, --password string   Password for the user to connect to database
  -p, --port int          Port number of the postgres database (default 5432)
  -u, --username string   Username to connect to the database

usage of command line arguments include

mock <sub command> <flags> -d <database-name> -u <user-name> -w <password> -p <port-number> -a <host-name or ip> 

for eg.s

mock database -f -d postgres -u postgres -p 3000 -a localhost -w postgres

Environment variable

If no arguments are supplied at the command line, then the tool checks for environment variables (i.e postgres default environment variables to connect to the database), these includes

  • PGDATABASE - for database to mock
  • PGUSER - for user to connect
  • PGPASSWORD - for password of the user
  • PGPORT - for the port to connect to database
  • PGHOSTADDR - for the hostname or ip to connect to

Usage of environment vairable

export PGDATABASE="<database-name>"
export PGUSER="<user-name>"
export PGPASSWORD="<password>"
export PGPORT="<port-number>"
export PGHOSTADDR="<hostname or ip>"
mock <subcommand> <flags>

for eg.s

export PGDATABASE="postgres"
export PGUSER="postgres"
export PGPASSWORD="postgres"
export PGPORT="3000"
export PGHOSTADDR="localhost"
mock database -f

Defaults

If no environment or command line arguments are provided then it set to default database, which is

 PGDATABASE="postgres"
 PGUSER="postgres"
 PGPASSWORD="postgres"
 PGPORT="5432"
 PGHOSTADDR="localhost"
Clone this wiki locally