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 9
  • 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 8
  • 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 8
  • 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

Save your terminal commands in bash history in real time
Use this command if you want your terminal commands be saved in your history file in real time instead of waiting until the terminal is closed

find and remove old compressed backup files
remove all compressed files in /home/ folder not created in the last 10 days

Perl One Liner to Generate a Random IP Address

Show errors in the kernel ring buffer
Much more useful then parsing syslog

Execute a command at a given time
This command will create a popup reminder window to assist in remembering tasks http://i.imgur.com/2n7viiA.png is how it looks when created

Check reverse DNS

colored tail

small CPU benchmark with PI, bc and time.
$ # 4 cores with 2500 pi digits $ CPUBENCH 4 2500 $. $ every core will use 100% cpu and you can see how fast they calculate it. $ if you do 50000 digitits and more it can take hours or days

Extract title from HTML files
previous version leaves lots of blank lines

Decrypt exported android wallet keys for import into desktop client (LTC,FTC,BTC)


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: