# bash completion for Postgresql -*- shell-script -*- proc _pg_databases { # -w was introduced in 8.4, https://launchpad.net/bugs/164772 # "Access privileges" in output may contain linefeeds, hence the NF > 1 setglobal COMPREPLY = ''( $( compgen -W "$( psql -XAtqwlF $'\t' 2>/dev/null | \ awk 'NF > 1 { print $1 }' )" -- "$cur" ) ) } proc _pg_users { # -w was introduced in 8.4, https://launchpad.net/bugs/164772 setglobal COMPREPLY = ''( $( compgen -W "$( psql -XAtqwc 'select usename from pg_user' \ template1 2>/dev/null )" -- "$cur" ) ) [[ ${#COMPREPLY[@]} -eq 0 ]] && setglobal COMPREPLY = ''( $( compgen -u -- "$cur" ) ) } # createdb(1) completion # proc _createdb { local cur prev words cword split _init_completion -s || return match $prev { with -h|--host _known_hosts_real $cur return 0 with -U|--username|-O|--owner _pg_users return 0 with -p|--port|-D|--tablespace|-E|--encoding|-T|--template # argument required but no completions available return 0 with --help|--version # all other arguments are noop with these return 0 } $split && return 0 if [[ "$cur" == -* ]] { setglobal COMPREPLY = ''( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace } else { _pg_databases } } && complete -F _createdb createdb # createuser(1) completion # proc _createuser { local cur prev words cword split _init_completion -s || return match $prev { with --help|--version|-p|--port|-c|--connection-limit return with -h|--host _known_hosts_real $cur return with -U|--username _pg_users return } $split && return if [[ "$cur" == -* ]] { setglobal COMPREPLY = ''( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace } } && complete -F _createuser createuser # dropdb(1) completion # proc _dropdb { local cur prev words cword split _init_completion -s || return match $prev { with -h|--host _known_hosts_real $cur return 0 with -U|--username _pg_users return 0 with --help|--version # all other arguments are noop with these return 0 } $split && return 0 if [[ "$cur" == -* ]] { setglobal COMPREPLY = ''( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace } else { _pg_databases } } && complete -F _dropdb dropdb # dropuser(1) completion # proc _dropuser { local cur prev words cword split _init_completion -s || return match $prev { with --help|--version|-p|--port return with -h|--host _known_hosts_real $cur return with -U|--username _pg_users return } $split && return if [[ "$cur" == -* ]] { setglobal COMPREPLY = ''( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace } else { _pg_users } } && complete -F _dropuser dropuser # psql(1) completion # proc _psql { local cur prev words cword split _init_completion -s || return match $prev { with -h|--host _known_hosts_real $cur return 0 with -U|--username _pg_users return 0 with -d|--dbname _pg_databases return 0 with -o|--output|-f|--file|-L|--log-file _filedir return 0 with -c|--command|-F|--field-separator|-p|--port|-P|--pset|\ -R|--record-separator|-T|--table-attr|-v|--set|--variable # argument required but no completions available return 0 with -\?|--help|-V|--version # all other arguments are noop with these return 0 } $split && return 0 if [[ "$cur" == -* ]] { # return list of available options setglobal COMPREPLY = ''( $( compgen -W '$( _parse_help "$1" )' -- "$cur" ) ) [[ $COMPREPLY == *= ]] && compopt -o nospace } else { # return list of available databases _pg_databases } } && complete -F _psql psql # ex: ts=4 sw=4 et filetype=sh