从 Liquibase XML 在 PostgreSQL 中创建带时区的时间戳
Create a timestamp with time zone in PostgreSQL from Liquibase XML
我有一个 Liquibase 迁移 XML 文件,它创建了 datetime
类型的列。
<createTable tableName="foo">
<column name="bar" type="datetime"/>
</createTable>
我今天惊愕地意识到,这些是在没有时区的情况下创建的(PostgreSQL 中的 timestamp without time zone
),而且似乎没有任何 Liquibase 类型可供您使用 timestamp with time zone
。在最初创建 table:
之后,除了像这样改变 table 的 <sql>
块之外,还有什么方法可以解决这个问题
<sql>
alter table foo alter column bar type timestamp with time zone;
</sql>
谢谢。
https://whosebug.com/users/330315/a-horse-with-no-name 在评论中提供的答案是使用 timestamptz
本机 PostgreSQL 类型:
<createTable tableName="foo">
<column name="bar" type="timestamptz"/>
</createTable>
TL;DR: 使用 timestamp with time zone
:
<column name="bar" type="timestamp with time zone"/>
Liquibase datetime
会自动转换为目标数据库的时间戳类型,但无法指定时区。
Liquibase 将接受 timestamp with time zone
并将其作为本机类型传递(无转换),但由于它是 SQL 标准类型,它无论如何都会被任何标准数据库接受(包括 PostgreSQL).
timestamptz
是 PostgreSQL 相同数据类型的特定缩写。不便携。
我有一个 Liquibase 迁移 XML 文件,它创建了 datetime
类型的列。
<createTable tableName="foo">
<column name="bar" type="datetime"/>
</createTable>
我今天惊愕地意识到,这些是在没有时区的情况下创建的(PostgreSQL 中的 timestamp without time zone
),而且似乎没有任何 Liquibase 类型可供您使用 timestamp with time zone
。在最初创建 table:
<sql>
块之外,还有什么方法可以解决这个问题
<sql>
alter table foo alter column bar type timestamp with time zone;
</sql>
谢谢。
https://whosebug.com/users/330315/a-horse-with-no-name 在评论中提供的答案是使用 timestamptz
本机 PostgreSQL 类型:
<createTable tableName="foo">
<column name="bar" type="timestamptz"/>
</createTable>
TL;DR: 使用 timestamp with time zone
:
<column name="bar" type="timestamp with time zone"/>
Liquibase datetime
会自动转换为目标数据库的时间戳类型,但无法指定时区。
Liquibase 将接受 timestamp with time zone
并将其作为本机类型传递(无转换),但由于它是 SQL 标准类型,它无论如何都会被任何标准数据库接受(包括 PostgreSQL).
timestamptz
是 PostgreSQL 相同数据类型的特定缩写。不便携。