Skip to content

Instantly share code, notes, and snippets.

@AlexeyKupershtokh
Last active March 21, 2024 02:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save AlexeyKupershtokh/82d04095a275e741785dcd6e6436643f to your computer and use it in GitHub Desktop.
Save AlexeyKupershtokh/82d04095a275e741785dcd6e6436643f to your computer and use it in GitHub Desktop.
Postgres DDL to Clickhouse converter
select
concat(
'create table ',
table_name,
'(',
string_agg(
concat(
column_name,
' ',
CASE when is_nullable = 'YES' THEN 'Nullable(' END,
CASE
when data_type = 'integer' then 'Int32'
when data_type = 'smallint' then 'Int16'
when data_type = 'bigint' then 'Int64'
when data_type = 'numeric'
then concat('Decimal(', numeric_precision, ',', numeric_scale, ')')
when data_type = 'double precision' then 'Float64'
when data_type = 'character varying' then 'String'
when data_type = 'character' then 'String'
when data_type = 'text' then 'String'
when data_type = 'boolean' then 'UInt8'
when data_type = 'date' then 'Date'
when data_type = 'timestamp without time zone' then 'Datetime'
when data_type = 'time without time zone' then 'String'
else concat('###################', data_type)
END,
CASE when is_nullable = 'YES' THEN ')' END
),
', ' order by ordinal_position
),
') ',
'ENGINE = MergeTree() '
'PARTITION BY id '
'ORDER BY [id] '
) as table_ddl
from information_schema.columns
where true
and table_catalog = 'unbiased_accounts'
and table_schema = 'public'
group by table_name;
select * from information_schema.columns
where true
and table_catalog = 'unbiased_accounts'
and table_schema = 'public'
--and data_type = 'time without time zone'
;
select distinct data_type from information_schema.columns
where true
and table_catalog = 'unbiased_accounts'
and table_schema = 'public';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment