Skip to content

Instantly share code, notes, and snippets.

@rossnelson
Forked from madelfio/postgres-queries.rst
Created April 28, 2012 18:04
Show Gist options
  • Save rossnelson/2520903 to your computer and use it in GitHub Desktop.
Save rossnelson/2520903 to your computer and use it in GitHub Desktop.
Useful Postgres Admin Queries

Useful Postgres Admin Queries

  • pg_stat_activity (currently executing queries):

    select * from pg_stat_activity where current_query not like '<%';
    
  • pg_user (all database users):

    select * from pg_user;
    
  • pg_database (all databases and their sizes):

    select datname,
           pg_size_pretty(pg_database_size(datname))
      from pg_database
     order by pg_database_size(datname) desc;
    
  • pg_tables (all tables and their size, with/without indexes):

    select tablename,
           pg_size_pretty(pg_relation_size(schemaname||'.'||tablename)) as size,
           pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) as total_size
      from pg_tables
     where schemaname = 'MY_SCHEMA_NAME';
    
  • pg_namespace (all schemas):

    select * from pg_namespace order by 1;
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment