Skip to content

Commit

Permalink
[#65] status command
Browse files Browse the repository at this point in the history
Implements the `status` command using `pg_ctl` as a way to check if the in-use instance is running or not.

Work implemented by @briansalehi revised by @theory and @fluca1978 .

Close #65
  • Loading branch information
briansalehi committed Feb 6, 2024
1 parent ea37034 commit 70af4d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,9 @@ Synopsis
# Start current version
pgenv start

# Show server status
pgenv status

# Restart current version
pgenv restart

Expand Down Expand Up @@ -516,6 +519,32 @@ It is possible to specify flags to pass to `pg_ctl(1)` when performing the
`restart` action, setting the `PGENV_RESTART_OPTIONS` array in the
[configuration](#pgenv-config).

### pgenv status

Indicates whether an instance is already *running* or is *stopped*. In case an
instance is not currently in use, the script complains and exits immediately.
Furthermore, you can use `pgenv current` to see which version is in use.

```sh
$ pgenv status
```

The above command results in an output like the following:

```
server is running (PID: 81803)
/opt/pgsql-16.0/bin/postgres "-D" "/opt/pgsql/data"
```

Same result can be achieved by running `pg_ctl` as follows:

```sh
$ $PG_ROOT/bin/pg_ctl status -D $PG_DATA
```

Where `PG_ROOT` is the path to your PostgreSQL installation directory, and
`PG_DATA` is the path to your database directory.

### pgenv available

Shows all the versions of PostgreSQL available to download and build. Handy to
Expand Down Expand Up @@ -601,6 +630,7 @@ The pgenv commands are:
start Start the current PostgreSQL server
stop Stop the current PostgreSQL server
restart Restart the current PostgreSQL server
status Show the current PostgreSQL server status
switch Set the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
build Build a specific version of PostgreSQL
Expand Down
12 changes: 11 additions & 1 deletion bin/pgenv
Expand Up @@ -146,6 +146,7 @@ The pgenv commands are:
start Start the current PostgreSQL server
stop Stop the current PostgreSQL server
restart Restart the current PostgreSQL server
status Show the current PostgreSQL server status
switch Set the current PostgreSQL version
clear Stop and unset the current PostgreSQL version
build Build a specific version of PostgreSQL
Expand Down Expand Up @@ -915,7 +916,7 @@ pgenv_start_instance(){
trap 'exit' ERR
}

# Initializes and instance if no data directory is found.
# Initializes an instance if no data directory is found.
# Requires PG_DATA to be set.
# It is safe to call multiple times.
pgenv_initdb(){
Expand Down Expand Up @@ -1168,6 +1169,15 @@ case $1 in
exit
;;

status)
# only proceed when an instance is loaded
pgenv_exit_if_no_postgresql_in_use
# print standard postgres status
$PG_CTL status -s -D "$PG_DATA" | sed -E 's/^pg_ctl:\s+//'
# instance is either running or stopped at this point
exit $?
;;

build|rebuild)
# load executables
pgenv_check_dependencies
Expand Down

0 comments on commit 70af4d4

Please sign in to comment.