From 98f5f1abc12dfa49ae5067a686d103a930a442e6 Mon Sep 17 00:00:00 2001 From: J Smith Date: Mon, 10 Sep 2012 00:59:18 -0400 Subject: [PATCH 1/5] Fix typo -- "oin" should be "oid". --- src/_pgsql_utils | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pgsql_utils b/src/_pgsql_utils index bdad9c8..2a8dffd 100644 --- a/src/_pgsql_utils +++ b/src/_pgsql_utils @@ -84,7 +84,7 @@ _pgsql_tables () { local _pgsql_table_sql _pgsql_table_sql="select n.nspname || '.' || c.relname \ from pg_catalog.pg_class c \ - left join pg_catalog.pg_namespace n on n.oin = c.relnamespace \ + left join pg_catalog.pg_namespace n on n.oid = c.relnamespace \ where c.relkind in ('r', '') \ and n.nspname <> 'pg_catalog' \ and n.nspname <> 'information_schema' \ From c5a5033e0810dde8858b2aa10b15a3a883fb95d7 Mon Sep 17 00:00:00 2001 From: J Smith Date: Mon, 10 Sep 2012 01:00:08 -0400 Subject: [PATCH 2/5] Connect to template1 when getting the list of databases. We can't assume that the user will have a database that shares their user name. --- src/_pgsql_utils | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/_pgsql_utils b/src/_pgsql_utils index 2a8dffd..19281b9 100644 --- a/src/_pgsql_utils +++ b/src/_pgsql_utils @@ -104,7 +104,7 @@ _pgsql_databases () { _pgsql_db_sql="select d.datname from pg_catalog.pg_database d \ where d.datname <> 'template0'" - compadd "$@" - $( psql $_pgsql_params[@] -Atq -c $_pgsql_db_sql 2>/dev/null ) + compadd "$@" - $( psql $_pgsql_params[@] -Atq -c $_pgsql_db_sql template1 2>/dev/null ) } From cfb8da6fb2e5c34040bed02ad22f7e8ef1b73b2e Mon Sep 17 00:00:00 2001 From: J Smith Date: Mon, 10 Sep 2012 01:01:38 -0400 Subject: [PATCH 3/5] Get the list of encodings from the database cluster. --- src/_pgsql_utils | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/_pgsql_utils b/src/_pgsql_utils index 19281b9..4ad55d9 100644 --- a/src/_pgsql_utils +++ b/src/_pgsql_utils @@ -107,6 +107,16 @@ _pgsql_databases () { compadd "$@" - $( psql $_pgsql_params[@] -Atq -c $_pgsql_db_sql template1 2>/dev/null ) } +_pgsql_encodings () { + local _pgsql_user + _pgsql_get_identity + + local _pgsql_db_sql + _pgsql_db_sql="select pg_encoding_to_char(i) from generate_series(0,100) i;" + + compadd "$@" - $( psql $_pgsql_params[@] -Atq -c $_pgsql_db_sql template1 ) +} + ## ## The actual completion code for the commands @@ -185,7 +195,7 @@ _createdb () { {-q,--quiet}'[non verbose mode]' \ {-D+,--location=}':database location:_directories' \ {-T+,--template=}':database template:_pgsql_databases' \ - {-E+,--encoding=}':database encoding:_values "encoding" $_pgsql_encodings[@]' \ + {-E+,--encoding=}':database encoding:_pgsql_encodings' \ ':PostgreSQL database:' \ ':comment:' } @@ -222,7 +232,7 @@ _vacuumdb () { } _pgsql_utils () { - local _pgsql_common_opts _pgsql_encodings + local _pgsql_common_opts _pgsql_common_opts=( {-\?,--help}'[display help]' @@ -232,25 +242,6 @@ _pgsql_utils () { {-W,--password}'[prompt for password]' ) - # Taken from - # . - # It'd be real nice if postgres could tell us these... - _pgsql_encodings=( - SQL_ASCII - EUC_{JP,CN,KR,TW} - JOHAB - UNICODE - MULE_INTERNAL - LATIN{1,2,3,4,5,6,7,8,9,10} - ISO_8859_{5,6,7,8} - KOI8 - WIN - ALT - WIN1256 - TCVN - WIN874 - ) - case "$service" in psql) _psql "$@" ;; pg_dump) _pg_dump "$@" ;; From 6b1c882e9dc7f3f69b1578fb0ba2e48877bc38ea Mon Sep 17 00:00:00 2001 From: J Smith Date: Mon, 10 Sep 2012 01:02:26 -0400 Subject: [PATCH 4/5] Add the createuser and dropuser commands. --- src/_pgsql_utils | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/_pgsql_utils b/src/_pgsql_utils index 4ad55d9..1b2d3b4 100644 --- a/src/_pgsql_utils +++ b/src/_pgsql_utils @@ -1,4 +1,4 @@ -#compdef psql pg_dump createdb dropdb vacuumdb +#compdef psql pg_dump createdb dropdb vacuumdb createuser dropuser # ------------------------------------------------------------------------------ # Description # ----------- @@ -231,6 +231,41 @@ _vacuumdb () { '1:PostgreSQL database:_pgsql_databases' } +_createuser () { + local curcontext="$curcontext" state line expl + typeset -A opt_args + + _arguments -C -s \ + "$_pgsql_common_opts[@]" \ + {-e,--echo}'[display SQL queries]' \ + {-c,--connection-limit=}'[connection limit for role (default: no limit)]' \ + {-d,--createdb}'[role can create new databases]' \ + {-D,--no-createdb}'[role cannot create databases]' \ + {-E,--encrypted}'[encrypt stored password]' \ + {-i,--inherit}'[role inherits privileges of roles it is a member of (default)]' \ + {-I,--no-inherit}'[role does not inherit privileges]' \ + {-l,--login}'[role can login (default)]' \ + {-L,--no-login}'[role cannot login]' \ + {-N,--unencrypted}'[do not encrypt stored password]' \ + {-P,--pwprompt}'[assign a password to new role]' \ + {-r,--createrole}'[role can create new roles]' \ + {-R,--no-createrole}'[role cannot create roles]' \ + {-s,--superuser}'[role will be superuser]' \ + {-S,--no-superuser}'[role will not be superuser]' +} + +_dropuser () { + local curcontext="$curcontext" state line expl + typeset -A opt_args + + _arguments -C -s \ + "$_pgsql_common_opts[@]" \ + {-e,--echo}'[display SQL queries]' \ + {-q,--quiet}'[non verbose mode]' \ + {-i,--interactive}'[confirm before drop]' \ + ':PostgreSQL user:_pgsql_users' +} + _pgsql_utils () { local _pgsql_common_opts @@ -248,6 +283,8 @@ _pgsql_utils () { createdb) _createdb "$@" ;; dropdb) _dropdb "$@" ;; vacuumdb) _vacuumdb "$@" ;; + createuser) _createuser "$@" ;; + dropuser) _dropuser "$@" ;; esac } From 603bd2f4e17745509d9e3500059e56cb5eac96de Mon Sep 17 00:00:00 2001 From: J Smith Date: Mon, 10 Sep 2012 01:02:50 -0400 Subject: [PATCH 5/5] Add the initdb command. --- src/_pgsql_utils | 49 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/_pgsql_utils b/src/_pgsql_utils index 1b2d3b4..1f95130 100644 --- a/src/_pgsql_utils +++ b/src/_pgsql_utils @@ -1,4 +1,4 @@ -#compdef psql pg_dump createdb dropdb vacuumdb createuser dropuser +#compdef psql pg_dump createdb dropdb vacuumdb createuser dropuser initdb # ------------------------------------------------------------------------------ # Description # ----------- @@ -266,8 +266,36 @@ _dropuser () { ':PostgreSQL user:_pgsql_users' } +_initdb () { + local curcontext="$curcontext" state line expl + typeset -A opt_args + + _arguments -C -s \ + {--auth=,-A+}':default authentication method for local connections:_values "auth methods" $_pgsql_auth_methods[@]' \ + {-D+,--pgdata=}':location for this database cluster:_files' \ + {-E+,--encoding=}':set default encoding for new databases:' \ + --locale=':set default locale for new databases:' \ + --lc-collate=':set the default locale for collate:' \ + --lc-ctype=':set the default locale for ctype:' \ + --lc-messages=':set the default locale for messages:' \ + --lc-monetary=':set the default locale for monetary:' \ + --lc-numeric=':set the default locale for numeric:' \ + --lc-time=':set the default local for time:' \ + --no-locale'[equivalent to --locale=C]' \ + --pwfile=':read password for the new superuser from file:_files' \ + {-T+,--text-search-config=}'[default text search configuration]' \ + {-U+,--username=NAME}':database superuser name:' \ + {-W,--pwprompt}'[prompt for a password for the new superuser]' \ + {-X+,--xlogdir=}':location for the transaction log directory:_files' \ + {-d,--debug}'[generate lots of debugging output]' \ + -L+':where to find the input files:_files' \ + {-n,--noclean}'[do not clean up after errors]' \ + {-s,--show}'[show internal settings]' \ + ':location for this database cluster:_files' +} + _pgsql_utils () { - local _pgsql_common_opts + local _pgsql_common_opts _pgsql_auth_methods _pgsql_common_opts=( {-\?,--help}'[display help]' @@ -277,6 +305,22 @@ _pgsql_utils () { {-W,--password}'[prompt for password]' ) + _pgsql_auth_methods=( + trust + reject + md5 + password + gss + sspi + krb5 + ident + peer + ldap + radius + cert + pam + ) + case "$service" in psql) _psql "$@" ;; pg_dump) _pg_dump "$@" ;; @@ -285,6 +329,7 @@ _pgsql_utils () { vacuumdb) _vacuumdb "$@" ;; createuser) _createuser "$@" ;; dropuser) _dropuser "$@" ;; + initdb) _initdb "$@" ;; esac }