Skip to content

Instantly share code, notes, and snippets.

@rxw1
Last active August 29, 2015 14:06
Show Gist options
  • Save rxw1/b6ae6c56d29c18d42fce to your computer and use it in GitHub Desktop.
Save rxw1/b6ae6c56d29c18d42fce to your computer and use it in GitHub Desktop.
PostgreSQL Queries
-- list all tables
SELECT c.relname FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','') AND n.nspname <> 'pg_catalog'AND n.nspname <> 'information_schema'AND n.nspname !~ '^pg_toast'AND pg_catalog.pg_get_userbyid(c.relowner) != 'postgres' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1;
-- list all columns for a table
SELECT attname FROM pg_attribute WHERE attrelid = 'public.sensor_tags'::regclass AND attnum > 0 AND NOT attisdropped ORDER BY attnum;
-- get all column types for a table
SELECT column_name, data_type FROM information_schema.columns WHERE table_name = '___TABLE_NAME___';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment