[TIL] How to connect to a remote postgresql without local psql

Nov 26 2019

This will open the psql terminal where execute our commands:

docker run -it -e PGPASSWORD=postgres --net=host \
    --entrypoint=psql postgres:11                \
    -h localhost -p 5432 -U postgres

Using a docker can be useful for:

  • Avoiding having to install the psql-client locally.
  • Ensuring the posgresql-client is in the same version as the database where we pretend to get connected.

We can indicate a command to be exeucted with -c option:

docker run -it -e PGPASSWORD=postgres --net=host \
    --entrypoint=psql postgres:11                \
    -h localhost -p 5432 -U postgres \
    -c 'SELECT pg_reload_conf();'