Skip to content

Connecting to Database

Faisal Ali edited this page Mar 10, 2021 · 5 revisions

Introduction

There are four ways to connect to database using this tool

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

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

URI

Use URI flag to connect to postgres database

--uri string        Postgres connection URI, eg. postgres://user:pass@host:=port/db?sslmode=disable

eg.s with uri

mock <sub command> <flags> --uri postgres://user:pass@host:port/db?sslmode=disable

If you have SSLMODE enabled on your database environment, then use URI to control the SSL options. For various options available with sslmode check this line on the file

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