Published:

Getting time from a remote database can be a simple way to maintain consistency across distributed systems, which is important for ensuring accurate logging and auditing of system activity.

REMOTE_IP=192.168.1.2
POSTGRES_USER=user
POSTGRES_DB=db
CONNECTION_CONFIG="-h $REMOTE_IP -U $POSTGRES_USER -d $POSTGRES_DB"
until pg_isready $CONNECTION_CONFIG; do sleep 1; done
psql $CONNECTION_CONFIG -c "select timeofday();" \
| sed -n 3p \
| xargs -i date -s '{}'

Rate this page