Commands tagged postgresql (12)

  • Continuously watches postgres, showing the instances using the most RAM at the top. Show Sample Output

    watch -n 1 '{ ps aux | head -n 1; ps aux --sort -rss | grep postgres | grep -v grep; } | cat'
    carbocation · 2017-05-14 17:01:44 0
  • to kill, use `kill PID` Credit: user Craig Ringer on, recommends to kill the process rather than deleting when there is an orphan Postgresql server process. Show Sample Output

    cat /usr/local/var/postgres/
    ctcrnitv · 2017-02-07 02:38:28 0
  • Check if SSH tunnel is open and open it, if it isn't. NB: In this example, 3333 would be your local port, 5432 the remote port (which is, afaik, usually used by PostgreSQL) and of course you should replace REMOTE_HOST with any valid IP or hostname. The example above let's you work on remote PostgreSQL databases from your local shell, like this: psql -E -h localhost -p 3333

    while true; do nc -z localhost 3333 >|/dev/null || (ssh -NfL 3333:REMOTE_HOST:5432 USER@REMOTE_HOST); sleep 15; done
    rxw · 2015-09-21 02:25:49 1
  • Replace the credentials to psql if necessary, and the my-query part with your query. Show Sample Output

    psql -U quassel quassel -c "SELECT message FROM backlog ORDER BY time DESC LIMIT 1000;" | grep my-query
    Tatsh · 2014-10-12 19:53:06 0
  • Without using a pipe. -X ignores the user's .psqlrc configuration file -A sets un-aligned table output mode -t prints rows only (no headers or footers) Show Sample Output

    psql -X -A -t -c "SELECT version();"
    malathion · 2014-05-01 18:10:20 0
  • -t, --tuples-only print rows only Show Sample Output

    psql -h <SERVER NAME HERE> -t -c 'SELECT version();' |head -1
    hxre · 2014-04-25 08:26:23 1
  • In a multiple PostgreSQL server environment knowing the servers version can be important. Note that psql --version returns just the local psql apps version which may not be what you want. This command dumps the PostgreSQL servers version out to one line. You may need to add more command line options to the psql command for your connection environment. Show Sample Output

    psql -h <SERVER NAME HERE> -c 'SELECT version();' | grep -v 'version\|---\|row\|^ *$' | sed 's/^\s*//'
    pnelsonsr · 2014-03-17 18:36:40 0
  • This command drops all the sequences of the 'public' schema from the database. First, it constructs a 'drop sequence' instruction for each table found in the schema, then it pipes the result to the psql interactive command. See it scripted here :

    psql -h <ph_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop sequence \"' || relname || '\" cascade;' from pg_class where relkind='S'" | psql -h <ph_host> -p <pg_port> -U <pg_user> <pg_db>
    cuberri · 2013-12-11 15:42:34 0
  • This command drops all the tables of the 'public' schema from the database. First, it constructs a 'drop table' instruction for each table found in the schema, then it pipes the result to the psql interactive command. Useful when you have to recreate your schema from scratch in development for example. I mainly use this command in conjunction with a similar command which drop all sequences as well. Example : psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'" | psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> psql -h <ph_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop sequence \"' || relname || '\" cascade;' from pg_class where relkind='S'" | psql -h <ph_host> -p <pg_port> -U <pg_user> <pg_db> See it scripted here :

    psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db> -t -c "select 'drop table \"' || tablename || '\" cascade;' from pg_tables where schemaname='public'" | psql -h <pg_host> -p <pg_port> -U <pg_user> <pg_db>
    cuberri · 2013-12-11 15:39:56 0
  • This command will "su" the execution of the command to the postgres user(implies that you are already logger as root), and export the result of the query to a file on the csv format. You'll need to adequate the fields and database information to one of your choice/need. Show Sample Output

    # su -c "psql -d maillog -c \"copy (select date,sender,destination,subject from maillog where destination like '') to '/tmp/mails.csv' with csv;\" " postgres
    Risthel · 2013-02-13 13:03:17 0
  • Get a listing of all of your databases in Postgres and their sizes, ordering by the largest size first. Requires that you give the -d parameter a valid database name that you can connect to. Show Sample Output

    psql -c "SELECT pg_database.datname, pg_database_size(pg_database.datname), pg_size_pretty(pg_database_size(pg_database.datname)) FROM pg_database ORDER BY pg_database_size DESC;" -d <ANYDBNAME>
    bbbco · 2011-11-30 15:22:48 2
  • It's certainly not nicely formatted SQL, but you can see the SQL in there...

    sudo tcpdump -nnvvXSs 1514 -i lo0 dst port 5432
    ethanmiller · 2009-12-18 17:12:44 1

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Check These Out

rsync with progress bar.
transfer files from localhost to a remotehost.

Inverted cowsay
It's quite fun to invert text using "" (ref: ). Slightly more challenging is to flip a whole "cowsay". :-)

Get AWS temporary credentials ready to export based on a MFA virtual appliance
You might want to secure your AWS operations requiring to use a MFA token. But then to use API or tools, you need to pass credentials generated with a MFA token. This commands asks you for the MFA code and retrieves these credentials using AWS Cli. To print the exports, you can use: `awk '{ print "export AWS_ACCESS_KEY_ID=\"" $1 "\"\n" "export AWS_SECRET_ACCESS_KEY=\"" $2 "\"\n" "export AWS_SESSION_TOKEN=\"" $3 "\"" }'` You must adapt the command line to include: * $MFA_IDis ARN of the virtual MFA or serial number of the physical one * TTL for the credentials

Create cheap and easy index.html file
If your admin has disabled Apache's directory index feature but you want to have a cheap way to enable it for one folder, this command will just create an index.html file with a link to each file in the directory (including the index.html file, which is not ideal but makes the command much simpler). The HTML isn't even remotely compliant, but it could easily be improved on. Also, the command needs to be run each time a file is added or removed to update the index.html file.

Convert entire audio library in parallel
Uses parallel processing Reiteration of my earlier command Usage lc Old_Directory New_DIrectory Old_Format New_Format lc ~/Music ~/Music_ogg mp3 ogg

Lvextend logical volume
this command extends your logical volume. then it does a resize2fs on it as well. added the -r switch to make it launch resize.

Unbelievable Shell Colors, Shading, Backgrounds, Effects for Non-X
I've been using linux for almost a decade and only recently discovered that most terminals like putty, xterm, xfree86, vt100, etc., support hundreds of shades of colors, backgrounds and text/terminal effects. This simply prints out a ton of them, the output is pretty amazing. If you use non-x terminals all the time like I do, it can really be helpful to know how to tweak colors and terminal capabilities. Like: $ echo $'\33[H\33[2J'

Find all symlinks that link to directories

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Command to logout all the users in one command
It's only to logout all other user's except "root"

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: