如何在 PGSQL 中将布尔列更改为位变化

How to change a boolean column to bit varying in PGSQL

create table test_div1( i boolean)
    insert into test_div1 values('1');

ALTER TABLE test_div1 ALTER i TYPE bit varying  USING (i::text::bit varying ); 

//不工作收到错误。 请帮忙

我认为你得到 error: "t" is not a valid binary digit,postgres 无法自动将 true 转换为 1

您需要做的就是像这样自己处理转换

create table test_div1( i boolean)

insert into test_div1 values('1');

insert into test_div1 values(false), ('1'), (true), ('0');

ALTER TABLE test_div1 ALTER i TYPE bit varying  USING (case when i = true then B'1' else B'0' end);

这是一个工作示例https://www.db-fiddle.com/f/wukNuibgNsJqzydNqz4eFy/0