A script for managing multiple local instances of a PostgreSQL server.
For local development, it can be helpful to keep the artifacts associated with a particular project isolated to a particular directory. Additionally, with version managers like asdf-vm, it’s possible to have multiple version of PostgreSQL available on a single machine.
Combined with direnv, pg_local
automates the process of running a
PostgreSQL for each project or client.
- macOS
- direnv — triggers the environment to be loaded
- Ruby — used to dynamically determine free port numbers
-
Place the
pg_local
script somewhere in your$PATH
. -
In each directory where you want an instance of PostgreSQL, run
pg_local setup
. This will add a line to your.envrc
(and allow it to be loaded).
Now, each time you enter this directory, the script will set PGDATA
and
PGPORT
to the match the local instance. Update your application
configuration to use these settings when it connects to the database. Commands
like psql
will read these values automatically, so no additional
configuration is needed there.
pg_local init
— initializes a local copy of PostgreSQLpg_local setup <version>
— hookspg_local into the
.envrc` in the current directorypg_local start
— starts the local PostgreSQL server with a dynamic port numberpg_local stop
— stops the local PostgreSQL serverpg_local restart
— restarts the local PostgreSQL serverpg_local status
— outputs whether the local server is running or notpg_local env
— exports thePGDATA
andPGPORT
environment variablespg_local upgrade <new_version>
— upgrades the local instance
- Fork it.
- Create your feature branch (
git checkout -b my-new-feature
). - Commit your changes (
git commit -am 'Added some feature'
). - Push to the branch (
git push origin my-new-feature
). - Create a new Pull Request.
Copyright © 2019 Tyler Hunt. See LICENSE for details.