J
J
Jamal Absalimov2020-05-17 23:46:00
PostgreSQL
Jamal Absalimov, 2020-05-17 23:46:00

Change column datatype in all tables?

There was a task to change the data type of the column created_atand updated_atfrom the type
timestamptzto normal timestampin all tables in the database, they were converted to this type as a result of dumpMysql migration using pgloader to the PostgreSQl database.
I decided to implement this with a sql query, but I have not yet found an answer to my question on the Internet. I would be grateful for your help

My request:

select 
 	 distinct 
string_agg(concat('ALTER_TABLE ', col.table_name, ' ALTER COLUMN ', col.column_name , ' TYPE timestamp '), ';') 
 FROM information_schema.columns col
    WHERE 
      col.table_catalog = 'api2' AND 
      col.table_schema = 'public' AND 
      col.column_name = 'created_at' or col.column_name = 'updated_at';


and option 2

select 
 	 distinct 'ALTER_TABLE ', col.table_name, ' ALTER COLUMN ', col.column_name , ' TYPE timestamp '
 FROM information_schema.columns col
    WHERE 
      col.table_catalog = 'api2' AND 
      col.table_schema = 'public' AND 
      col.column_name = 'created_at' or col.column_name = 'updated_at';

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question