PostgreSQL – How to List Tables Used by A View

To list the tables used by a view in PostgreSQL, you can query the system catalog to inspect the view’s underlying SQL query. PostgreSQL stores view definitions in the pg_views system catalog table. Here’s how you can retrieve the tables used by a specific view: SELECT table_name FROM information_schema.tables WHERE table_schema = ‘public’ AND table_name…

PostgreSQL – How to Extract Date from Timestamp

You can extract date from a timestamp using various functions depending on the specific part of the date you want to extract. Here are some commonly used functions for extracting different parts of a date from a timestamp: Extracting Date Only (Year, Month, and Day):To extract only the date (year, month, and day) from a…

PostgreSQL – How to Get DDL of a Materialized View

To retrieve the Data Definition Language (DDL) for a materialized view in PostgreSQL, you can use the pg_get_viewdef function, which is a built-in function for getting the definition (DDL) of various database objects, including materialized views. Here’s the syntax to retrieve the DDL for a materialized view: SELECT pg_get_viewdef(‘your_materialized_view_name’::regclass, true); where your_materialized_view_name is the name…

PostgreSQL – How to List All Materialized Views

To list the materialized views in a PostgreSQL database, you can query the system catalog tables. Materialized views are stored as objects in the database, and you can query the catalog to retrieve information about them. Here’s a query to list all materialized views in a PostgreSQL database: SELECT schemaname, matviewname FROM pg_matviews; Above query…

PostgreSQL – How to Retrieve the Size of A Database

To get the size of a PostgreSQL database, you can use a few different methods, including SQL queries and command-line utilities. Here are a couple of approaches: Using SQL Query You can use the following SQL query to get the size of a specific database in PostgreSQL: SELECT pg_size_pretty(pg_database_size(‘your_database_name’)) AS database_size; where your_database_name is the…