This can help if you need to run frequent queries from the command line (not from psql cli).
Add this to .bash_profile / .bashrc
POSTGRES_BIN=~/Postgres/bin B_RED='\033[1;31m' RESET='\033[0m' psqlcommand="$POSTGRES_BIN/psql -U vignesh usersdb -q -c" function psqlselectrows() { [ -z "$1" ] && echo -e "${B_RED}Argument 1 missing: Need table name${RESET}" || $psqlcommand "SELECT * from $1" }
The above command selects the rows from the table passed in the argument.
Remarks:
- Change the database name as required.
- The default schema is public. To have a different default scheme, add the following line to the
~/.psqlrc .
SET SEARCH_PATH TO <schema_name>;
- If the database is password protected, refer to this and use the secure method.
I have made several commands for my use if this can help.
- psqlselectrows - to select rows from a table
- psqlgettablecount - get the number of rows in a table
- psqltruncatetable - crop table on request
- psqlgettablesize - get table size
- psqlgetvacuumdetails - get vacuum table details
- psqlsettings - to get the default settings and changed settings configured for Postgres.
(All of the above commands need a table name as the first argument)
#Colors B_RED='\033[1;31m' B_GREEN='\033[1;32m' B_YELLOW='\033[1;33m' RESET='\033[0m' #Postgres Command With Params psqlcommand="$POSTGRES_BIN/psql -U vignesh usersdb -q -c" function psqlgettablesize() { [ -z "$1" ] && echo -e "${B_RED}Argument 1 missing: Need table name${RESET}" || $psqlcommand "select pg_size_pretty(pg_total_relation_size('$1')) as total_table_size, pg_size_pretty(pg_relation_size('$1')) as table_size, pg_size_pretty(pg_indexes_size('$1')) as index_size;"; } function psqlgettablecount() { [ -z "$1" ] && echo -e "${B_RED}Argument 1 missing: Need table name${RESET}" || $psqlcommand "select count(*) from $1;" } function psqlgetvacuumdetails() { [ -z "$1" ] && echo -e "${B_RED}Argument 1 missing: Need table name${RESET}" || $psqlcommand "SELECT relname, n_live_tup, n_dead_tup, last_analyze::timestamp, analyze_count, last_autoanalyze::timestamp, autoanalyze_count, last_vacuum::timestamp, vacuum_count, last_autovacuum::timestamp, autovacuum_count FROM pg_stat_user_tables where relname='$1' and schemaname = current_schema();" } function psqltruncatetable() { [ -z "$1" ] && echo -e "${B_RED}Argument 1 missing: Need table name${RESET}" || { read -p "$(echo -e ${B_YELLOW}"Are you sure to truncate table '$1' (y/n)? "${RESET})" choice case "$choice" in y|Y ) $psqlcommand "TRUNCATE $1;";; n|N ) echo -e "${B_GREEN}Table '$1' not truncated${RESET}";; * ) echo -e "${B_RED}Invalid option${RESET}";; esac } } function psqlsettings() { query="select * from pg_settings" if [ "$1" != "" ]; then query="$query where category like '%$1%'" fi query="$query ;" $psqlcommand "$query" if [ -z "$1" ]; then echo -e "${B_YELLOW}Passing Category as first argument will filter the related settings.${RESET}" fi } function psqlselectrows() { [ -z "$1" ] && echo -e "${B_RED}Argument 1 missing: Need table name${RESET}" || $psqlcommand "SELECT * from $1" }
source share