Connect to a database

Open an interactive psql session to mydb on localhost

psql -h localhost -U myuser -d mydb

tool
postgres29 commands
tags
postgres

More postgres commands

  • psql -U postgres -c '\l+'

    Show all databases with their size and encoding

  • psql -U myuser -d mydb -c '\dt public.*'

    List all tables in the public schema

  • psql -U myuser -d mydb -c 'EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT * FROM orders WHERE status = $1;'

    Run the query and show its plan with real timings and buffer usage

  • psql -U postgres -d mydb -c "SELECT round(total_exec_time::numeric,2) ms, calls, round((total_exec_time/calls)::numeric,2) avg_ms, query FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 10;"

    Top 10 queries by total execution time, with call count and average time

  • psql -U postgres -c "SELECT pid, now()-query_start AS duration, state, left(query,80) FROM pg_stat_activity WHERE state <> 'idle' ORDER BY duration DESC;"

    Show running queries, how long they run and the first 80 characters of SQL

all 29 postgres commands