Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

postgresql - Postgres is adding a space at the beginning and end of all fields

SLES 12 SP3
Postgres 10.8

I have duplicated a table to migrate data from a DB2 instance. The fields are all of type CHAR, VARCHAR, or TIMESTAMP. I originally tried to use COPY to pull the data in from a pipe delimited file. But, it put a space at the beginning and end of all of the fields, even if this caused the field to be longer than it is defined. I found a claim online that this was a known issue with COPY. At that point, I dropped the table, used sed and some other tools to convert the pipe delimited data into an SQL INSERT statement. I again had a leading and trailing space in every field.

There are a lot of columns but as an example of what I have follows:

FLD1  CHAR(6) PRIMARY KEY
FLD2  VARCHAR(8)
FLD3  TIMESTAMP

I am using the short form of INSERT.

INSERT INTO my_table VALUES
('123456', '12345678', '2021-01-01 12:34:56');

But when I do a SELECT, I get (note the leading and trailing spaces):

 123456 | 12345678 | 2021-01-01 12:34:56 |

I would point out that the first two fields are now longer than they are defined by 2 characters.

Does anyone how I might fix this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The -A argument to psql gives me the desired result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...