select * from pg_tables where tableowner = 'postgres';
alter user usertochange with password 'new_passwd';
alter user mysuper with superuser;
# or even better
alter user mysuper with SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION
# readonly to all tables for myuser
grant select on all tables in schema public to myuser;
# all privileges on table1 and table2 to myuser
grant all privileges on table1, table2, table3 to myuser;
pg_restore --verbose --clean --no-acl --no-owner -h localhost -U myuser -d mydb latest.dump
-- List procedure/function
SELECT * FROM pg_proc WHERE proname='__procedurename__';
-- List view (including the definition)
SELECT * FROM pg_views WHERE viewname='__viewname__';
-- Show DB table space in use
SELECT pg_size_pretty(pg_total_relation_size('__table_name__'));:
-- Show DB space in use
SELECT pg_size_pretty(pg_database_size('__database_name__'));
-- Show current user's statement timeout
show statement_timeout;
-- Show table indexes
SELECT * FROM pg_indexes WHERE tablename='__table_name__' AND schemaname='__schema_name__';
-- Get all indexes from all tables of a schema:
SELECT
t.relname AS table_name,
i.relname AS index_name,
a.attname AS column_name
FROM
pg_class t,
pg_class i,
pg_index ix,
pg_attribute a,
pg_namespace n
WHERE
t.oid = ix.indrelid
AND i.oid = ix.indexrelid
AND a.attrelid = t.oid
AND a.attnum = ANY(ix.indkey)
AND t.relnamespace = n.oid
AND n.nspname = 'kartones'
ORDER BY
t.relname,
i.relname
-- Queries being executed at a certain DB
SELECT datname, application_name, pid, backend_start, query_start, state_change, state, query
FROM pg_stat_activity
WHERE datname='__database_name__';
-- Get all queries from all dbs waiting for data (might be hung)
SELECT * FROM pg_stat_activity WHERE waiting='t';
-- See the query plan for the given query
EXPLAIN __query__
-- See and execute the query plan for the given query
EXPLAIN ANALYZE __query__
-- Collect statistics
ANALYZE [__table__]
-- Query data in columns c1, c2 from a table
SELECT c1, c2 FROM t;
-- Query distinct rows from a table
SELECT DISTINCT c1
FROM t
WHERE condition;
-- Sort the result set in ascending or descending order
SELECT c1, c2
FROM t
ORDER BY c1 ASC [DESC];
-- Skip offset of rows and return the next n rows
SELECT c1, c2
FROM t
ORDER BY c1
LIMIT n
OFFSET offset;
-- Group rows using an aggregate function
SELECT c1, aggregate(c2)
FROM t
GROUP BY c1;
-- Filter groups using HAVING clause
SELECT c1, aggregate(c2) FROM t
GROUP BY c1
HAVING condition;
-- Inner join t1 and t2
SELECT c1, c2
FROM t1
INNER JOIN t2
ON condition;
-- Left join t1 and t1
SELECT c1, c2
FROM t1
LEFT JOIN t2
ON condition;
-- Right join t1 and t2
SELECT c1, c2
FROM t1
RIGHT JOIN t2
ON condition;
-- Perform full outer join
SELECT c1, c2
FROM t1
FULL OUTER JOIN t2
ON condition;
-- Produce a Cartesian product of rows in tables
SELECT c1, c2
FROM t1
CROSS JOIN t2;
-- Another way to perform cross join
SELECT c1, c2
FROM t1, t2;
-- Join t1 to itself using INNER JOIN clause
SELECT c1, c2
FROM t1 A
INNER JOIN t2 B ON condition
-- Combine rows from two queries
SELECT c1, c2 FROM t1
UNION [ALL]
SELECT c1, c2 FROM t2;
-- Return the intersection of two queries
SELECT c1, c2 FROM t1
INTERSECT
SELECT c1, c2 FROM t2;
-- Subtract a result set from another result set
SELECT c1, c2 FROM t1
EXCEPT
SELECT c1, c2 FROM t2;
-- Query rows using pattern matching %, _
SELECT c1, c2 FROM t1
WHERE c1 [NOT] LIKE pattern;
-- Query rows in a list
SELECT c1, c2
FROM t
WHERE c1
[NOT] IN value_list;
-- Query rows between two values
SELECT c1, c2
FROM t
WHERE c1
BETWEEN low AND high;
-- Check if values in a table is NULL or not
SELECT c1, c2 FROM t
WHERE c1 IS [NOT] NULL;