Commands tagged postgresql (12)

  • 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


    6
    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 6
  • It's certainly not nicely formatted SQL, but you can see the SQL in there...


    1
    sudo tcpdump -nnvvXSs 1514 -i lo0 dst port 5432
    ethanmiller · 2009-12-18 17:12:44 5
  • 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


    0
    # su -c "psql -d maillog -c \"copy (select date,sender,destination,subject from maillog where destination like '%domain.com%') to '/tmp/mails.csv' with csv;\" " postgres
    Risthel · 2013-02-13 13:03:17 5
  • 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 : https://gist.github.com/cuberri/6868774#file-postgresql-drop-create-sh


    0
    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 15
  • 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 : https://gist.github.com/cuberri/6868774#file-postgresql-drop-create-sh


    0
    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 7
  • 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


    0
    psql -h <SERVER NAME HERE> -c 'SELECT version();' | grep -v 'version\|---\|row\|^ *$' | sed 's/^\s*//'
    pnelsonsr · 2014-03-17 18:36:40 8
  • -t, --tuples-only print rows only Show Sample Output


    0
    psql -h <SERVER NAME HERE> -t -c 'SELECT version();' |head -1
    hxre · 2014-04-25 08:26:23 8
  • 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


    0
    psql -X -A -t -c "SELECT version();"
    malathion · 2014-05-01 18:10:20 7
  • Replace the credentials to psql if necessary, and the my-query part with your query. Show Sample Output


    0
    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 7
  • to kill, use `kill PID` Credit: user Craig Ringer on stackexchange.com, recommends to kill the process rather than deleting postmaster.pid when there is an orphan Postgresql server process. Show Sample Output


    0
    cat /usr/local/var/postgres/postmaster.pid
    ctcrnitv · 2017-02-07 02:38:28 19
  • Continuously watches postgres, showing the instances using the most RAM at the top. Show Sample Output


    0
    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 16
  • 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


    -1
    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 11

What's this?

commandlinefu.com 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

Find usb device in realtime
Using this command you can track a moment when usb device was attached.

Edit a file on a remote host using vim

Save your open windows to a file so they can be opened after you restart
This will save your open windows to a file (~/.windows). To start those applications: $ cat ~/.windows | while read line; do $line &; done Should work on any EWMH/NetWM compatible X Window Manager. If you use DWM or another Window Manager not using EWMH or NetWM try this: $ xwininfo -root -children | grep '^ ' | grep -v children | grep -v '' | sed -n 's/^ *\(0x[0-9a-f]*\) .*/\1/p' | uniq | while read line; do xprop -id $line _NET_WM_PID | sed -n 's/.* = \([0-9]*\)$/\1/p'; done | uniq -u | grep -v '^$' | while read line; do ps -o cmd= $line; done > ~/.windows

Escape forward slashes in a variable
For example $ path="/etc/apt/sources.list"; echo ${path//'/'/'\/'} will print $ \/etc\/apt\/sources.list

zgrep across multiple files

list block devices
Shows all block devices in a tree with descruptions of what they are.

Separates each frame of a animated gif file to a counted file, then appends the frames together into one sheet file. Useful for making sprite sheets for games.
requires imagemagick

See n most used commands in your bash history
You can append these commands to the bottom of the history file to access them easier with the Up key: $ sort ~/.bash_history|uniq -c|sort -n|tail -n 10|tr -s " "|cut -d' ' -f3- >> ~/.bash_history

Redirect tar extract to another directory
The command extracting the tar contents into particular directory ...

Have subversion ignore a file pattern in a directory
If you don't want to commit files to subversion, and don't want those file to show up when doing an "svn stat", this command is what you need


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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: