Pages

Wednesday, July 15, 2015

aliasing psql

When you run psql without arguments, then the psql tries to connect to database named like current user. I dislike this behave. Surely I have not database "pavel". There is trivial solution - add to your .bashrc code:
function psql { 
if [[ $# -eq 0 ]]; then
   env psql postgres
else
  env psql "$@"
fi
}

3 comments:

  1. Alternatively use the ENV:

    export PGDATABASE=postgres

    in .bashrc/.zshrc or similar

    ReplyDelete
  2. @anonymous: good to know, thank you

    ReplyDelete
  3. Similarly, you can set things like PGSERVICE in your environment if you're using psql to access remote systems most of the time. There are a fair number of other customizations of this type. http://www.postgresql.org/docs/9.5/static/app-psql.html#AEN9442

    ReplyDelete