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

时间:2018-09-12 13:54:47

标签: 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)
...

经过一段时间的搜索,我认为我应该将列类型从oid更改为bytea。我在用户界面pgAdmin中尝试过此操作:

ALTER TABLE theTableName
ALTER COLUMN xxxx TYPE bytea

但是我得到了错误:

ERROR:  column "xxxx" cannot be cast automatically to type bytea
HINT:  You might need to specify "USING xxxx::bytea".
SQL state: 42804

然后我尝试:

ALTER TABLE theTableName
ALTER COLUMN xxxx TYPE bytea USING xxxx::bytea

我得到了这个错误:

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

请问该如何解决?非常感谢!

1 个答案:

答案 0 :(得分:3)

先投射到TEXT,然后投射到BYTEA

ALTER TABLE theTableName
ALTER COLUMN xxxx TYPE bytea USING xxxx::TEXT::BYTEA