Skip to content

Instantly share code, notes, and snippets.

@matanper
Created September 7, 2024 10:04
Show Gist options
  • Save matanper/605cfd88cad26cf3ef2370d58abdf70f to your computer and use it in GitHub Desktop.
Save matanper/605cfd88cad26cf3ef2370d58abdf70f to your computer and use it in GitHub Desktop.
Useful PG commands
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;
CREATE USER replication_user PASSWORD 'xxx';
GRANT USAGE ON SCHEMA "public" TO replication_user;
GRANT SELECT ON ALL TABLES IN SCHEMA "public" TO replication_user;
ALTER DEFAULT PRIVILEGES IN SCHEMA "public" GRANT SELECT ON TABLES TO replication_user;
GRANT rds_replication TO replication_user;
CREATE PUBLICATION publication_name FOR table x, y;
SELECT relname AS table_name,
n_live_tup,
n_dead_tup,
n_dead_tup::float / (n_live_tup + n_dead_tup) * 100 AS dead_tuple_percentage
FROM pg_stat_all_tables
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER api_pg
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host 'host', port '5432', dbname 'db');
CREATE USER MAPPING FOR CURRENT_USER
SERVER api_pg
OPTIONS (user 'read-only', password 'pass');
create schema api_external;
CREATE FOREIGN TABLE api_external.table_name (
created_at_ms int8,
id uuid
)
SERVER api_pg
OPTIONS (schema_name 'public', table_name 'table_name');
GRANT USAGE ON schema api_external TO CURRENT_USER;
grant select on api_external.some_table to CURRENT_USER;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment