如何在 PostgreSQL 中将列类型从 oid 更改为 bytea

Spi*_*der 2 postgresql

首先,我收到了这个错误:

org.postgresql.util.PSQLException: ERROR: column xxxx is of type oid but expression is of type bytea
Hint: You will need to rewrite or cast the expression.
Position: 318
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
...
Run Code Online (Sandbox Code Playgroud)

在谷歌搜索一段时间后,我认为我应该将列类型从 oid 更改为 bytea。我在 UI pgAdmin 中尝试了这个:

ALTER TABLE theTableName
ALTER COLUMN xxxx TYPE bytea
Run Code Online (Sandbox Code Playgroud)

但我得到了错误:

ERROR:  column "xxxx" cannot be cast automatically to type bytea
HINT:  You might need to specify "USING xxxx::bytea".
SQL state: 42804
Run Code Online (Sandbox Code Playgroud)

然后我尝试:

ALTER TABLE theTableName
ALTER COLUMN xxxx TYPE bytea USING xxxx::bytea
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误:

ERROR:  cannot cast type oid to bytea
LINE 2: ALTER COLUMN xxxx TYPE bytea USING xxxx::bytea
                                                ^
SQL state: 42846
Character: 88
Run Code Online (Sandbox Code Playgroud)
  • 系统:Windows 10
  • PostgreSQL 版本:10
  • pgAdmin 4 版本:3.0
  • Python 版本 2.7.11
  • 烧瓶版本:0.12.2

请问如何解决这个问题?非常感谢!

404*_*404 6

投射到TEXT然后到BYTEA

ALTER TABLE theTableName
ALTER COLUMN xxxx TYPE bytea USING xxxx::TEXT::BYTEA
Run Code Online (Sandbox Code Playgroud)

警告

请注意,这不会神奇地迁移任何数据!因此,pg_largobject由存储在 oid 列中的值引用的大对象不会复制到theTableName表中。只在空表上运行这个,或者如果你能承受丢失大对象。