2

I have table tbl_mak in SQL Server which contain's the following data:

Table:

CREATE TABLE tbl_mak
(
   col_bit bit 
);

insert into tbl_mak values(1);
insert into tbl_mak values(0);

After exporting into Flat File (.txt) i got the following value's:

col_bit
True
False

And I have the following table in PostgreSQL 9.5:

CREATE TABLE tbl_mak
(
   col_bit bit 
);

Now i want to import the exported data from SQL Server into PostgreSQL.

Script:

\COPY tbl_mak FROM 'D:\mak.txt'

Getting an error:

ERROR:  bit string length 5 does not match type bit(1)
1
  • Use 0/1 instead of true,false Commented Jan 16, 2017 at 9:27

3 Answers 3

4

Create a table like below; Use boolean type for the field

CREATE TABLE tbl_mak
(
   col_bit boolean
);

and import data from text file like below

COPY tbl_mak FROM 'D:\mak.txt' WITH CSV HEADER DELIMITER AS ' '
Sign up to request clarification or add additional context in comments.

5 Comments

Does this solution import 0/1 as same as in SQL server in PostgreSQL?
Because i don't want to store something like t/f in place of 0/1.
You can import boolean values either True/False or 0/1, and Postgres stores boolean values as t/f
FYI, If you want to get stored boolean values(t/f) as 0/1 use select col_bit::int from tbl_mak
Can you please help me for this: stackoverflow.com/questions/45273829/…
0

In Postgresql the bit datatype is different from SQL-Server in postgres it is

bit [ (n) ]   fixed-length bit string

but in SQL-server its a

integer data type that can take a value of 1, 0, or NULL

so in the file its TRUE/FALSE and when you are trying to import the data in postgresql its getting an error for more please go through the link

Comments

0

From Postgres docs

Writing bit without a length is equivalent to bit(1)

So mention the length

CREATE TABLE tbl_mak
(
   col_bit bit(5)
);

I think you can use Boolean Type instead of BIT

Valid literal values for the "true" state are:

TRUE
't'
'true'
'y'
'yes'
'1'

For the "false" state, the following values can be used:

FALSE
'f'
'false'
'n'
'no'
'0'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.