A convenient way to launch psql against postgres while running pg_regress

October 10, 2019
A convenient way to launch psql against postgres while running pg_regress

I got tired of looking up regression_output/log/postmaster.log to find the PGSOCKET to use to connect to a running pg_regress‘s temp-install postgres so I wrote a little shell function for it. My patch to print a connst in pg_regress never got merged so I needed a workaround.

I’m sure I’m not the only one, so here’s regress_psql for your convenience.

Pop it in your bashrc and invoke it from the directory you run make check in.

 # Launch a psql against a running pg_regress instance
 # without messing around reading the logs.
 #
 function regress_psql() {
     pgpidf="tmp_check/data/postmaster.pid"
     if ! [ -e "$pgpidf" ]
     then
         echo "no postmaster.pid at $pgpidf"
         return 1
     fi
  
     PGPORT="$(awk 'NR==4 { print $0; }' "$pgpidf")"
     PGHOST="$(awk 'NR==5 { print $0; }' "$pgpidf")"
     connstr="host='$PGHOST' port=$PGPORT dbname='postgres'"
     echo "connstr: \"$connstr\""
     psql -v PROMPT1="%M:%> [%p] %n@%/=%# " -v PROMPT2="%M:%> [%p] %n@%/+%# " -q "$connstr" "$@"
}

Usage:

$ regress_psql 
connstr: "host='/tmp/pg_regress-SdmFxp' port=60853 dbname='postgres'"
[local:/tmp/pg_regress-SdmFxp]:60853 [30043] craig@postgres=# 

As a side note, I recommend keeping such aliases in a separate ~/.bash_aliases that you can source from your .bashrc. It’s easier to reload just the aliases then.

Share this

More Blogs