I needed to change all tables columns in the whole DB from data type timestamp
without
time zone
to timestamp(0)
with
time zone
.
So I created this function:
CREATE OR REPLACE FUNCTION grd_replace_datetime_with_timezone()
RETURNS character varying AS
$BODY$
DECLARE
old_column RECORD;
s text;
BEGIN
FOR old_column IN (
SELECT
isc.table_schema as table_schema,
isc.table_name as table_name,
isc.column_name as column_name
FROM
information_schema.columns isc
INNER JOIN
pg_tables pt
ON (isc.table_schema = pt.schemaname and isc.table_name = pt.tablename)
WHERE
isc.column_name like %date% and
isc.table_schema in ( public ) and
isc.data_type = timestamp without time zone
ORDER BY
isc.table_name ASC
)
LOOP
RAISE NOTICE Schema: % ,old_column.table_schema;
RAISE NOTICE Table: % ,old_column.table_name;
RAISE NOTICE Column % ,old_column.column_name;
EXECUTE ALTER TABLE ||old_column.table_schema|| . ||old_column.table_name||
ALTER COLUMN ||old_column.column_name|| TYPE timestamp(0) with time zone ;
RAISE NOTICE ------------------------------------------------------------------------------- ;
RAISE NOTICE ;
END LOOP;
RETURN S ;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
但有些观点取决于受影响的栏目,
ERROR: cannot alter type of a column used by a view or rule
我对指数也有错误
如何更改所有 timestamp 的数据类型, 没有时区
列?