Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

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:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged postgresql from sorted by
Terminal - Commands tagged postgresql - 9 results
psql -U quassel quassel -c "SELECT message FROM backlog ORDER BY time DESC LIMIT 1000;" | grep my-query
2014-10-12 19:53:06
User: Tatsh
Functions: grep
0

Replace the credentials to psql if necessary, and the my-query part with your query.

psql -X -A -t -c "SELECT version();"
2014-05-01 18:10:20
User: malathion
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)

psql -h <SERVER NAME HERE> -t -c 'SELECT version();' |head -1
psql -h <SERVER NAME HERE> -c 'SELECT version();' | grep -v 'version\|---\|row\|^ *$' | sed 's/^\s*//'
2014-03-17 18:36:40
User: pnelsonsr
Functions: grep sed
0

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.

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>
2013-12-11 15:42:34
User: cuberri
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 : https://gist.github.com/cuberri/6868774#file-postgresql-drop-create-sh

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>
2013-12-11 15:39:56
User: cuberri
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 : https://gist.github.com/cuberri/6868774#file-postgresql-drop-create-sh

# 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
2013-02-13 13:03:17
User: Risthel
Functions: su
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.

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>
2011-11-30 15:22:48
User: bbbco
4

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.

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

It's certainly not nicely formatted SQL, but you can see the SQL in there...