假设我在数据库中创建了一些用户定义的类型,
Say I have created some user-defined types in the DB,
即创建类型 abc ...
那么是否可以确定用户定义的类型是否存在?也许,使用任何 postgres 信息表?
Is it then possible to determine if the user-defined type exists or not? Perhaps, using any of the postgres information tables?
这样做的主要原因是因为 PostgreSQL 似乎不支持 CREATE OR REPLACE TYPE ...,并且如果某个类型被创建多次,我希望能够删除首先现有的,然后重新加载新的.
The main reason for this is since PostgreSQL does not seem to support CREATE OR REPLACE TYPE ..., and if a certain type gets created more than once, I want to be able to drop the existing one first, then re-load the new one.
推荐答案我在这里添加了在简单脚本中创建类型的完整解决方案,而无需为此目的创建函数.
I add here the complete solution for creating types in a simple script, without the need of creating a function just for this purpose.
--create types DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'my_type') THEN CREATE TYPE my_type AS ( --my fields here... ); END IF; --more types here... END$$;更多推荐
检查用户定义的类型是否已存在于 PostgreSQL 中
发布评论