You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ClinicalTrialsDataProcessing/export/export_data_postgres_tables...

25 lines
803 B
SQL

SELECT
'CREATE TABLE ' || schemaname || '.' || tablename || E'\n(\n' ||
string_agg(column_definition, E',\n') || E'\n);\n'
FROM (
SELECT
schemaname,
tablename,
column_name || ' ' || data_type ||
CASE
WHEN character_maximum_length IS NOT NULL THEN '(' || character_maximum_length || ')'
ELSE ''
END ||
CASE
WHEN is_nullable = 'NO' THEN ' NOT NULL'
ELSE ''
END as column_definition
FROM pg_catalog.pg_tables t
JOIN information_schema.columns c
ON t.schemaname = c.table_schema
AND t.tablename = c.table_name
WHERE schemaname != 'pg_catalog'
and schemaname != 'information_schema'-- Replace with your schema name
) t
GROUP BY schemaname, tablename;