diff --git a/README.md b/README.md index b07f216..6bb7af8 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1082 TILs and counting..._ +_1083 TILs and counting..._ --- @@ -507,6 +507,7 @@ _1082 TILs and counting..._ - [Between Symmetric](postgres/between-symmetric.md) - [Capitalize All The Words](postgres/capitalize-all-the-words.md) - [Change The Current Directory For psql](postgres/change-the-current-directory-for-psql.md) +- [Check If The Local Server Is Running](postgres/check-if-the-local-server-is-running.md) - [Check Table For Any Oprhaned Records](postgres/check-table-for-any-orphaned-records.md) - [Checking Inequality](postgres/checking-inequality.md) - [Checking The Type Of A Value](postgres/checking-the-type-of-a-value.md) diff --git a/postgres/check-if-the-local-server-is-running.md b/postgres/check-if-the-local-server-is-running.md new file mode 100644 index 0000000..c01f9d8 --- /dev/null +++ b/postgres/check-if-the-local-server-is-running.md @@ -0,0 +1,43 @@ +# Check If The Local Server Is Running + +An install of PostgreSQL comes with a number of utilities including the +`pg_isready` command. This command can be used to check if the local PostgreSQL +server is up, running, and ready to receive connections. + +If the server has not yet been started, running the command will result in a +`no response` response. + +```bash +$ pg_isready +localhost:5432 - no response +``` + +In this case, the `pg_ctl` command can be used to start the server. + +```bash +$ pg_ctl -D $HOME/.asdf/installs/postgres/12.3/data start +waiting for server to start.... + +... + + done +server started +``` + +It tells us that the server is started and we can confirm that by again running +`pg_isready`. + +```bash +$ pg_isready +localhost:5432 - accepting connections +``` + +This command is most useful as part of a script, such as in a CI environment. +In that case, you may not want it writing to `stdout`, you just want to use the +command's exit code. For that, you can tack on the `--quiet` flag. + +``` +$ pg_isready --quiet +``` + +[source](https://www.postgresql.org/docs/current/app-pg-isready.html)