Development scripts

Most development operations can be effectively optimized using shell scripts. This page describes various scripts that can help in database development and debugging.

During development/debugging you will be working with specific database version and installation, because you do not want to mess everything up with different versions.

We will use the following architecture:

  • ./ - project root with all source files
    • ./build - binary installation directory: bin, share, lib, include
    • ./data - directory with database installation (PGDATA)
    • ./scripts - special development scripts (this section describes them)
      • build.sh - running configure script and building database
      • run.sh - managing database installation: create, start, stop, etc...
      • clean.sh - cleaning files: database, build artifacts, etc...
      • env.sh - generated file with exported environmental variables

Each file contains it's own --help flag which show available options and usage examples.

Scripts can be found in GitHub repository.

build.sh

In previous section we discussed how the database is compiled, so this script will do that - run configure and build database.

But that's not all. Usually you will work with single server, no multi-server cluster. So it would be more convenient to just run pg_ctl start or initdb that will work with the specified installation or just type psql to quickly connect to server.

So, after configure script has executed we will create env file, that will store PostgreSQL special environmental variables.

Taking into an account default values for database/user/password/etc... we can generate env file in the following way:

PGINSTDIR="$(pwd)/build"
./configure --prefix="$PGINSTDIR" # ...

# shell env file
SHENVFILE="$(pwd)/scripts/env.sh"
cat <<EOF >"$SHENVFILE"
export PGINSTDIR="$PGINSTDIR"
export PGDATA="$(pwd)/data"
export PGHOST="localhost"
export PGPORT="5432"
export PGUSER="postgres"
export PGDATABASE="postgres"
export PATH="$PGINSTDIR/bin:\$PATH"
LD_LIBRARY_PATH="\${LD_LIBRARY_PATH:-''}"
export LD_LIBRARY_PATH="\$PGINSTDIR/lib:\$LD_LIBRARY_PATH"
EOF

Using this file you can just source it and then use all binaries inside database installation path that will work with specific database installation.

source ./scripts/env.sh

# These commands use binaries under "./build/bin"
# and work with database in "./data"
initdb
pg_ctl start

run.sh

There are 4 main operations with database:

  • create
  • start
  • connect using psql
  • stop

Script run.sh will support all these operations and will be just a wrapper around: initdb, pg_ctl and psql.

clean.sh

As the name implies, this script is responsible for cleaning.

It cleans:

  • build artifacts
  • installation files
  • created database

There is no magic in this file.