Меню

Ошибка неверный синтаксис для типа integer

Running COPY results in ERROR: invalid input syntax for integer: "" error message for me. What am I missing?

My /tmp/people.csv file:

"age","first_name","last_name"
"23","Ivan","Poupkine"
"","Eugene","Pirogov"

My /tmp/csv_test.sql file:

CREATE TABLE people (
  age        integer,
  first_name varchar(20),
  last_name  varchar(20)
);

COPY people
FROM '/tmp/people.csv'
WITH (
  FORMAT CSV,
  HEADER true,
  NULL ''
);

DROP TABLE people;

Output:

$ psql postgres -f /tmp/sql_test.sql
CREATE TABLE
psql:sql_test.sql:13: ERROR:  invalid input syntax for integer: ""
CONTEXT:  COPY people, line 3, column age: ""
DROP TABLE

Trivia:

  • PostgreSQL 9.2.4

asked Aug 18, 2013 at 10:08

gmile's user avatar

gmilegmile

14.3k13 gold badges63 silver badges97 bronze badges

1

ERROR: invalid input syntax for integer: «»

"" isn’t a valid integer. PostgreSQL accepts unquoted blank fields as null by default in CSV, but "" would be like writing:

SELECT ''::integer;

and fail for the same reason.

If you want to deal with CSV that has things like quoted empty strings for null integers, you’ll need to feed it to PostgreSQL via a pre-processor that can neaten it up a bit. PostgreSQL’s CSV input doesn’t understand all the weird and wonderful possible abuses of CSV.

Options include:

  • Loading it in a spreadsheet and exporting sane CSV;
  • Using the Python csv module, Perl Text::CSV, etc to pre-process it;
  • Using Perl/Python/whatever to load the CSV and insert it directly into the DB
  • Using an ETL tool like CloverETL, Talend Studio, or Pentaho Kettle

answered Aug 18, 2013 at 10:57

Craig Ringer's user avatar

Craig RingerCraig Ringer

297k71 gold badges666 silver badges754 bronze badges

2

I think it’s better to change your csv file like:

"age","first_name","last_name"
23,Ivan,Poupkine
,Eugene,Pirogov

It’s also possible to define your table like

CREATE TABLE people (
  age        varchar(20),
  first_name varchar(20),
  last_name  varchar(20)
);

and after copy, you can convert empty strings:

select nullif(age, '')::int as age, first_name, last_name
from people

answered Aug 18, 2013 at 10:58

Roman Pekar's user avatar

Roman PekarRoman Pekar

104k27 gold badges190 silver badges195 bronze badges

1

Just came across this while looking for a solution and wanted to add I was able to solve the issue by adding the «null» parameter to the copy_from call:

cur.copy_from(f, tablename, sep=',', null='')

answered Sep 19, 2019 at 12:38

helderreis's user avatar

helderreishelderreis

1011 silver badge3 bronze badges

1

I got this error when loading ‘|’ separated CSV file although there were no ‘»‘ characters in my input file. It turned out that I forgot to specify FORMAT:

COPY … FROM … WITH (FORMAT CSV, DELIMITER ‘|’).

answered Mar 11, 2018 at 0:09

Slobodan Savkovic's user avatar

0

Use the below command to copy data from CSV in a single line without casting and changing your datatype.
Please replace «NULL» by your string which creating error in copy data

copy table_name from 'path to csv file' (format csv, null "NULL", DELIMITER ',', HEADER);

answered Dec 31, 2019 at 9:25

Anil's user avatar

AnilAnil

1231 silver badge7 bronze badges

1

I had this same error on a postgres .sql file with a COPY statement, but my file was tab-separated instead of comma-separated and quoted.

My mistake was that I eagerly copy/pasted the file contents from github, but in that process all the tabs were converted to spaces, hence the error. I had to download and save the raw file to get a good copy.

answered Aug 31, 2015 at 15:02

zwippie's user avatar

zwippiezwippie

14.8k3 gold badges38 silver badges53 bronze badges

CREATE TABLE people (
  first_name varchar(20),
  age        integer,
  last_name  varchar(20)
);

«first_name»,»age»,»last_name»
Ivan,23,Poupkine
Eugene,,Pirogov

copy people from 'file.csv' with (delimiter ‘;’, null »);

select * from people;

Just in first column…..

Feras Al Sous's user avatar

answered Oct 16, 2018 at 12:06

WSchnuble's user avatar

1

Ended up doing this using csvfix:

csvfix map -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv

In case you know for sure which columns were meant to be integer or float, you can specify just them:

csvfix map -f 1 -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv

Without specifying the exact columns, one may experience an obvious side-effect, where a blank string will be turned into a string with a 0 character.

answered Aug 18, 2013 at 14:10

gmile's user avatar

gmilegmile

14.3k13 gold badges63 silver badges97 bronze badges

2

this ought to work without you modifying the source csv file:

alter table people alter column age type text;
copy people from '/tmp/people.csv' with csv;

answered Aug 24, 2017 at 18:05

soyayix's user avatar

soyayixsoyayix

1671 silver badge7 bronze badges

0

There is a way to solve «», the quoted null string as null in integer column,
use FORCE_NULL option :

copy table_name FROM 'file.csv' with (FORMAT CSV, FORCE_NULL(column_name));

see postgresql document, https://www.postgresql.org/docs/current/static/sql-copy.html

answered Oct 1, 2018 at 22:14

Charlie 木匠's user avatar

Charlie 木匠Charlie 木匠

2,07618 silver badges19 bronze badges

All in python (using psycopg2), create the empty table first then use copy_expert to load the csv into it. It should handle for empty values.

import psycopg2
conn = psycopg2.connect(host="hosturl", database="db_name", user="username", password="password")
cur = conn.cursor()
cur.execute("CREATE TABLE schema.destination_table ("
            "age integer, "
            "first_name varchar(20), "
            "last_name varchar(20)"
            ");")

with open(r'C:/tmp/people.csv', 'r') as f:
    next(f)  # Skip the header row. Or remove this line if csv has no header.
    conn.cursor.copy_expert("""COPY schema.destination_table FROM STDIN WITH (FORMAT CSV)""", f)

answered Nov 4, 2020 at 16:40

Theo F's user avatar

Theo FTheo F

1,0671 gold badge10 silver badges18 bronze badges

Incredibly, my solution to the same error was to just re-arrange the columns. For anyone else doing the above solutions and still not getting past the error.

I apparently had to arrange the columns in my CSV file to match the same sequence in the table listing in PGADmin.

answered Oct 17, 2020 at 21:13

user3507825's user avatar

user3507825user3507825

4045 silver badges13 bronze badges

#javascript #node.js #postgresql #express #server

Вопрос:

Надеюсь, что кто-нибудь может помочь со следующей ошибкой экспресс-сервера, поскольку не уверен, что искать и почему этот тип ошибок продолжает появляться. Он использует серверную часть базы данных postgres. Похоже, это также приводит к сбою сервера.

 error: invalid input syntax for type integer: ""
    at Parser.parseErrorMessage (/home/app/server/node_modules/pg-protocol/dist/parser.js:278:15)
    at Parser.handlePacket (/home/app/server/node_modules/pg-protocol/dist/parser.js:126:29)
    at Parser.parse (/home/app/server/node_modules/pg-protocol/dist/parser.js:39:38)
    at Socket.<anonymous> (/home/app/server/node_modules/pg-protocol/dist/index.js:10:42)
    at Socket.emit (events.js:375:28)
    at addChunk (internal/streams/readable.js:290:12)
    at readableAddChunk (internal/streams/readable.js:265:9)
    at Socket.Readable.push (internal/streams/readable.js:204:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  length: 101,
  severity: 'ERROR',
  code: '22P02',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'numutils.c',
  line: '323',
  routine: 'pg_strtoint32'
}
 

Есть ли что-то конкретное, что мне нужно искать на основе этого error: invalid input syntax for type integer ?

ОБНОВЛЕНО — основываясь на приведенном ниже комментарии, может ли следующий запрос вставки вызвать эту ошибку?

 const insertLogQuery = `
insert into log (log_id, run_id, s_id, q_id, message)
     values ($1, $2, $3, $4, $5)          
`;
const insertLog = await pool.query(insertLogQuery, [ id, null, null, null, reason ]);
 

где столбцы: run_id , s_id и q_id определяются как целые числа.

Если да, может ли кто-нибудь, пожалуйста, сообщить мне, каков наилучший подход для предотвращения этой ошибки на основе этого кода.

Комментарии:

1. ДА. Вы должны искать преобразования из строковых типов данных integer в свои (нераскрытые) запросы. Ваши данные где-то будут содержать пустую строку.

2. @LaurenzAlbe — Я добавил запрос на вставку, который, как я думаю, может вызвать проблему, основываясь на том, что вы упомянули. Не уверен, как это исправить, хотя, если это проблема.

3. Вы должны использовать процесс, известный как «отладка». Выясните, какой столбец и какая инструкция SQL вызывает ошибку, определите фактическую выданную инструкцию SQL и параметры (например, в файле журнала PostgreSQL). И так далее.

If you have experienced something similar & can create a reproducible example please open a new issue.


When opening an issue, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This boils down to ensuring your code that reproduces the problem follows the following guidelines:

  • Minimal – Use as little code as possible that still produces the same problem
  • Complete – Provide all parts someone else needs to reproduce your problem in the question itself
  • Reproducible – Test the code you’re about to provide to make sure it reproduces the problem

Minimal

The more code there is to go through, the less likely people can find your problem. Streamline your example in one of two ways:

  1. Restart from scratch. Create a new program, adding in only what is needed to see the problem. Use simple, descriptive names for functions and variables – don’t copy the names you’re using in your existing code.
  2. Divide and conquer. If you’re not sure what the source of the problem is, start removing code a bit at a time until the problem disappears – then add the last part back.

Don’t sacrifice clarity for brevity when creating a minimal example. Use consistent naming and indentation, and include code comments if needed. Use your code editor’s shortcut for formatting code.

Don’t include any passwords or credentials that must be kept secret.

Complete

Make sure all information necessary to reproduce the problem is included in the issue itself.

If the problem requires some code as well as some XML-based configuration, include code for both. The problem might not be in the code that you think it is in.

Use individual code blocks for each file or snippet you include. Provide a description for the purpose of each block.

DO NOT use images of code. Copy the actual text from your code editor, paste it into the issus, then format it as code. This helps others more easily read and test your code.

Reproducible

To help you solve your problem, others will need to verify that it exists.

Describe the problem. «It doesn’t work» isn’t descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question.

Eliminate any issues that aren’t relevant to the problem. If your question isn’t about a compiler error, ensure that there are no compile-time errors.

Double-check that your example reproduces the problem! If you inadvertently fixed the problem while composing the example but didn’t test it again, you’d want to know that before asking someone else to help.

The UNION result set data types will be determined by the logic that favours values that are not string literals — it is described in detail in the manual, also note that, without an explicit type specification, string literals are treated as being of type unknown.

Specifically, these rules apply in your case:

5. Choose the first non-unknown input type which is a preferred type in
that category, if there is one.

and

7. Convert all inputs to the selected type. Fail if there is not a conversion from a given input to the selected type.

This basically means that if you have at least one numeric literal value in a particular column (in your case fourth) in your UNIONised query, Postgres will attempt to coerce the values in the same column in other SELECTs into a number, which obviously fails for the character value '2017-01-01'.

Subsequently, if you have at least one character value in that column that cannot be cast into a number, you will have to use explicit character values for the fourth column in all SELECTs:

insert into configurations
(id, key, description, value)
select 1,'key1','D1', '25'
UNION
select 2,'key2','D2', '365'
UNION
select 3,'key3','D3','2017-01-01'

H/T to Andriy M for his comment that prompted me to read the manual more thoroughly.

под управлением COPY результаты ERROR: invalid input syntax for integer: "" сообщение об ошибке для меня. Что я упускаю?

мой :

"age","first_name","last_name"
"23","Ivan","Poupkine"
"","Eugene","Pirogov"

мой :

CREATE TABLE people (
  age        integer,
  first_name varchar(20),
  last_name  varchar(20)
);

COPY people
FROM '/tmp/people.csv'
WITH (
  FORMAT CSV,
  HEADER true,
  NULL ''
);

DROP TABLE people;

выход:

$ psql postgres -f /tmp/sql_test.sql
CREATE TABLE
psql:sql_test.sql:13: ERROR:  invalid input syntax for integer: ""
CONTEXT:  COPY people, line 3, column age: ""
DROP TABLE

Общая информация:

  • PostgreSQL 9.2.4

7 ответов


ошибка: неверный входной синтаксис для integer: «»

"" не является допустимым числом. В PostgreSQL можно без кавычек пустые поля как null по умолчанию в CSV, но "" было бы похоже на написание:

SELECT ''::integer;

и не по той же причине.

если вы хотите иметь дело с CSV, который имеет такие вещи, как цитируемые пустые строки для целых чисел null, вам нужно будет передать его PostgreSQL через предпроцессор, который может немного его подправить. CSV-вход PostgreSQL не понимает всех странных и замечательных возможных злоупотреблений CSV.

варианты:

  • загрузка его в электронную таблицу и экспорт вменяемого CSV;
  • использование Python csv модуль для Perl Text::CSV и т. д. Для предварительной обработки;
  • использование Perl / Python / whatever для загрузки CSV и вставки его непосредственно в DB
  • используя инструмент ETL, как CloverETL, Talend Studio, или Pentaho чайник

Я думаю, что лучше изменить csv-файл, например:

"age","first_name","last_name"
23,Ivan,Poupkine
,Eugene,Pirogov

также можно определить вашу таблицу как

CREATE TABLE people (
  age        varchar(20),
  first_name varchar(20),
  last_name  varchar(20)
);

и после копирования, вы можете обменять пустые строки:

select nullif(age, '')::int as age, first_name, last_name
from people

у меня была такая же ошибка на postgres с COPY утверждение, но мой файл tab-разделены вместо запятую и в кавычках.

моя ошибка заключалась в том, что я охотно копировал/вставлял содержимое файла из github, но в этом процессе все вкладки были преобразованы в пробелы, следовательно, ошибка. Мне пришлось загрузить и сохранить raw-файл, чтобы получить хорошую копию.


в конечном итоге это делается с помощью csvfix:

csvfix map -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv

в случае, если вы точно знаете, какие столбцы должны быть integer или float, вы можете указать только их:

csvfix map -f 1 -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv

без указания точных столбцов может возникнуть очевидный побочный эффект, когда пустая строка будет превращена в строку с 0 символ.


это должно работать без изменения исходного csv-файла:

alter table people alter column age type text;
copy people from '/tmp/people.csv' with csv;

Я получил эту ошибку при загрузке » / «разделенного CSV-файла, хотя в моем входном файле не было символов»». Оказалось, что я забыл указать формат:

копировать … ОТ… С (ФОРМАТ CSV, разделитель ‘|’).

1

автор: Slobodan Savkovic



Запуск COPY приводит к ERROR: invalid input syntax for integer: "" сообщению об ошибке. Что мне не хватает?

Мой файл /tmp/people.csv:

"age","first_name","last_name"
"23","Ivan","Poupkine"
"","Eugene","Pirogov"

Мой файл /tmp/csv_test.sql:

CREATE TABLE people (
  age        integer,
  first_name varchar(20),
  last_name  varchar(20)
);

COPY people
FROM '/tmp/people.csv'
WITH (
  FORMAT CSV,
  HEADER true,
  NULL ''
);

DROP TABLE people;

Выход:

$ psql postgres -f /tmp/sql_test.sql
CREATE TABLE
psql:sql_test.sql:13: ERROR:  invalid input syntax for integer: ""
CONTEXT:  COPY people, line 3, column age: ""
DROP TABLE

Общая информация:

  • PostgreSQL 9.2.4

Ответ 1

ОШИБКА: неверный синтаксис ввода для целого: «»

"" не является допустимым целым числом. PostgreSQL принимает неучтенные пустые поля как null по умолчанию в CSV, но "" будет похож на запись:

SELECT ''::integer;

и сбой по той же причине.

Если вы хотите иметь дело с CSV, который имеет такие вещи, как процитированные пустые строки для нулевых целых чисел, вам нужно будет отправить его в PostgreSQL через предварительный процессор, который может немного его поднять. PostgreSQL CSV вход не понимает всех странных и замечательных возможных злоупотреблений CSV.

Параметры включают:

  • Загрузка его в электронную таблицу и экспорт нормального CSV;
  • Использование модуля Python csv, Perl Text::CSV и т.д. для его предварительной обработки;
  • Использование Perl/Python/что угодно для загрузки CSV и вставки его непосредственно в DB
  • Использование инструмента ETL, такого как CloverETL, Talend Studio или Pentaho Kettle.

Ответ 2

Я думаю, что лучше изменить файл csv, например:

"age","first_name","last_name"
23,Ivan,Poupkine
,Eugene,Pirogov

Также можно определить таблицу как

CREATE TABLE people (
  age        varchar(20),
  first_name varchar(20),
  last_name  varchar(20)
);

и после копирования вы можете преобразовать пустые строки:

select nullif(age, '')::int as age, first_name, last_name
from people

Ответ 3

У меня была такая же ошибка в файле postgres .sql с инструкцией COPY, но мой файл был разделен на разделы, а не разделен запятыми и кавычками.

Моя ошибка заключалась в том, что я с готовностью копировал/вставлял содержимое файла из github, но в этом процессе все вкладки были преобразованы в пробелы, следовательно, ошибка. Мне пришлось загрузить и сохранить исходный файл, чтобы получить хорошую копию.

Ответ 4

Я получил эту ошибку при загрузке ‘|’ если в моем исходном файле не было символов » ‘. Оказалось, что я забыл указать FORMAT:

COPY… FROM… WITH ( ФОРМАТ CSV, DELIMITER ‘|’).

Ответ 5

Закончено делать это с помощью csvfix:

csvfix map -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv

Если вы точно знаете, какие столбцы должны были быть integer или float, вы можете указать только их:

csvfix map -f 1 -fv '' -tv '0' /tmp/people.csv > /tmp/people_fixed.csv

Без указания точных столбцов может возникнуть очевидный побочный эффект, когда пустая строка будет преобразована в строку с символом 0.

Ответ 6

Есть способ решить «», заключенная в кавычки пустая строка как ноль в целочисленном столбце,
используйте параметр FORCE_NULL:

copy table_name FROM 'file.csv' with (FORMAT CSV, FORCE_NULL(column_name));

см. документ postgresql, https://www.postgresql.org/docs/current/static/sql-copy.html

Ответ 7

это должно работать без изменения исходного файла csv:

alter table people alter column age type text;
copy people from '/tmp/people.csv' with csv;

Ответ 8

CREATE TABLE people (
  first_name varchar(20),
  age        integer,
  last_name  varchar(20)
);

«first_name», «возраст», «last_name»
Иван, 23, Poupkine
Евгений Пирогов ,

скопировать людей из 'file.csv' с помощью (разделителя ‘;’, null »);

select * from people;

Просто в первом столбце…..

Ответ 9

Просто наткнулся на это, когда искал решение, и хотел добавить, что мне удалось решить эту проблему, добавив параметр «null» в вызов copy_from:

cur.copy_from(f, tablename, sep=',', null='')

0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии

А вот еще интересные материалы:

  • Яшка сломя голову остановился исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного исправьте ошибки
  • Ясность цели позволяет целеустремленно добиваться намеченного где ошибка
  • Ошибка неверный синтаксис для типа boolean
  • Ошибка невозможно установить безопасное соединение во всех браузерах