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
It's certainly not nicely formatted SQL, but you can see the SQL in there...
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
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
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
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
-t, --tuples-only print rows only Show Sample Output
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
Replace the credentials to psql if necessary, and the my-query part with your query. Show Sample Output
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
Continuously watches postgres, showing the instances using the most RAM at the top. Show Sample Output
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
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.
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
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: