\set ECHO queries2. execute the script using the -e flag
psql -e -f myfile.sql
Minimalistic Oracle contains a collection of practical examples from my encounters with Oracle technologies. When relevant, I also write about other technologies, like Linux or PostgreSQL. Many of the posts starts with "how to" since they derive directly from my own personal experience. My goal is to provide simple examples, so that they can be easily adapted to other situations.
\set ECHO queries2. execute the script using the -e flag
psql -e -f myfile.sql
SELECT conname FROM pg_constraint WHERE conrelid = 'mytable'::regclass; conname ---------------- sys_c002733781 sys_c002733782 (2 rows)2. Drop them:
ALTER TABLE mytable DROP CONSTRAINT sys_c002733781; ALTER TABLE ALTER TABLE mytable DROP CONSTRAINT sys_c002733782; ALTER TABLE3. Load your data using your preferred method
alter table mytable add constraint sys_c002733781 foreign key(job_id) references jobs(id) on delete cascade not valid; ALTER TABLE alter table mytable add constraint sys_c002733782 foreign key(task_id) references tasks(id) on delete cascade not valid; ALTER TABLE5. Validate constraints:
alter table mytable validate constraint sys_c002733781; ALTER TABLE alter table mytable validate constraint sys_c002733782; ALTER TABLE
SELECT schema_name, object_name, object_type FROM ( -- Tables, Views, Indexes, etc. SELECT n.nspname AS schema_name, c.relname AS object_name, CASE c.relkind WHEN 'r' THEN 'TABLE' WHEN 'v' THEN 'VIEW' WHEN 'm' THEN 'MATERIALIZED VIEW' WHEN 'i' THEN 'INDEX' WHEN 'S' THEN 'SEQUENCE' WHEN 'f' THEN 'FOREIGN TABLE' WHEN 'p' THEN 'PARTITIONED TABLE' WHEN 'I' THEN 'PARTITIONED INDEX' ELSE c.relkind::text END AS object_type FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE n.nspname = current_schema() UNION ALL -- Functions SELECT n.nspname AS schema_name, p.proname AS object_name, 'FUNCTION' AS object_type FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE n.nspname = current_schema() ) AS objects ORDER BY object_type, object_name;It only shows objects in the current schema (like Oracle USER_OBJECTS).
WHERE n.nspname = current_schema()with
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
psql -h postgres01.oric.no -U mysuperuser postgres -c "COPY (SELECT name || '=' || setting FROM pg_settings ORDER BY name) TO STDOUT WITH CSV HEADER" > pg_config.csv
SELECT s.schemaname, s.relname AS table_name, s.n_live_tup, s.last_analyze, s.last_autoanalyze FROM pg_stat_all_tables s JOIN pg_class c ON c.relname = s.relname LEFT JOIN pg_inherits i ON i.inhrelid = c.oid OR i.inhparent = c.oid WHERE s.schemaname = 'myschema' AND ( s.relname = 'mytable' OR c.oid IN ( SELECT inhrelid FROM pg_inherits WHERE inhparent = ( SELECT oid FROM pg_class WHERE relname = 'mytable' ) ) ) ORDER BY s.n_live_tup DESC,s.last_analyze;Example output
schemaname | table_name | n_live_tup | last_analyze | last_autoanalyze -----------------+-----------------+------------+-------------------------------+------------------------------ myschema | mytable_2015_02 | 95788115 | 2025-08-06 12:58:22.952592+00 | myschema | mytable_2015_03 | 78505350 | 2025-08-06 12:58:37.147433+00 | myschema | mytable_2015_04 | 71211253 | 2025-08-06 12:58:51.311452+00 | myschema | mytable_2015_01 | 68255510 | 2025-08-06 12:58:08.42708+00 | myschema | mytable_2015_05 | 62075043 | 2025-08-06 12:59:05.434118+00 | myschema | mytable_2015_06 | 60888876 | 2025-08-06 12:59:19.918657+00 | 2025-08-06 11:31:19.551345+00 myschema | mytable_2015_12 | 57485594 | 2025-08-06 13:00:43.112316+00 | 2025-08-27 03:13:11.394606+00 myschema | mytable_2016_01 | 57405307 | 2025-08-06 13:00:55.395113+00 | 2025-08-25 13:35:07.749773+00
SELECT pg_current_wal_lsn(); -- On the primaryIn my case, the result was
pg_current_wal_lsn -------------------- 0/E015420On the standby, run
SELECT pg_last_wal_replay_lsn();In my case, the result was
pg_last_wal_replay_lsn ------------------------ 0/E015420
psql -h hostname -U username -d databaseOptions:
Option Description -h <host> Hostname or IP address of the PostgreSQL server -p <port> Port number (default is 5432) -U <username> Username to connect as -d <dbname> Database name -W Prompt for password (useful if password auth is required) -f <filename> Execute commands from a SQL script file -c "<command>" Execute a single SQL command and exit -v var=value Set psql variable (used inside scripts) --set=var=value Same as -v, more explicit --dbname=<connstr> Full connection string, e.g.: postgresql://user:pass@host:port/dbnameExample
psql -h pg01.oric.no -p 5432 -U myuser -d mydbAlternatively, use an URL, which is more scripting friendly:
psql "postgresql://replicate@pg01.oric.no:5432/mydb"