Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - check if postgres is ready

Tags:

I have a Kong API Gateway container and a postgres container and I need to check whether postgres has started up and ready from the Kong container before running the migrations. I was thinking of installing the postgres client utilities into a custom image based on the official Kong image using RUN yum install postgresql -y && yum clean all in my Dockerfile and using either psql or pg_isready to achieve this. I've created a postgres user called polling with an empty password specifically for checking the status of the server by these two utilities. Neither of them work.

I tried to execute these commands from the custom Kong image:

  1. psql. The command psql -h postgres -U polling -w -c '\l' fails with the error psql: fe_sendauth: no password supplied. But the user has no password. What am I doing wrong? The full shell script checking whether the server is ready using psql is described here.

  2. pg_isready. I don't get how to install this utility separately into a custom image based on the official Kong image which in turn based on the centos:7 image, the postgresql package doesn't include pg_isready. Only these utilities are installed and can be found in /usr/bin: pg_config, pg_dump, pg_dumpall, pg_restore, psql. How to install pg_isready? I don't want to have the full server installation in the Kong image.

like image 442
super.t Avatar asked Oct 01 '17 20:10

super.t


People also ask

How can I tell if Postgres is running?

basically just type "systemctl status postgresql-xx" where xx is the version of your PostgreSQL instance. ex: systemctl status posgresql-10.

What is Pg_isready in PostgreSQL?

pg_isready is a utility for checking the connection status of a PostgreSQL database server. The exit status specifies the result of the connection check.

Where is var lib PostgreSQL data?

This optional variable can be used to define another location - like a subdirectory - for the database files. The default is /var/lib/postgresql/data .


1 Answers

Here is a shell one liner using pg_isready tool provided by PostgreSQL.

To call outside docker:

DOCKER_CONTAINER_NAME="mypgcontainer"
timeout 90s bash -c "until docker exec $DOCKER_CONTAINER_NAME pg_isready ; do sleep 5 ; done"

Based on a post from Jeroma Belleman.

like image 95
Mikko Ohtamaa Avatar answered Oct 01 '22 20:10

Mikko Ohtamaa