Importing .csv to postgres. what to do with time? - postgresql

I would like to import some .csv data into postgres and have issues with a data type :
one of my attributes is birthday:
1968-06-24 00:00:00
Therefore I use timestamp, as suggested by pgmodeler. However I always get the message:
postgres=# \connect new_database
You are now connected to database "new_database" as user "postgres".
new_database=# \copy players FROM '/Users/Desktop/Rdaten/Data/players.csv' DELIMITER ';' CSV HEADER
Error:
ERROR: invalid input syntax for type timestamp: "NULL"
CONTEXT: COPY players, line 267, column birthday: "NULL"
new_database=#
What can I do about that?

Not sure if this is the same issue, but I got this error trying to import a CSV where null values were listed as 'NULL' and fixed it by adding "null 'NULL'" to the copy command--by default Postgres expects NULL values to be input as empty strings.

Related

How can I manually insert a .png or .jpeg file into bytea column in PostgreSQL?

I'm currently using PostgreSQL 4.38 on Windows 10, I'm trying to insert an image (.png file) manually into bytea column in PostgreSQL, but upon executing the data, it says that invalid input syntax for type bytea. What should be the right procedure to insert an image manually in bytea column in PostgreSQL?
The error I am getting is
ERROR: invalid input syntax for type bytea
LINE 3: ...yn', 'Lyn', '123456', '234567', 'lone#gmail.com', 'D:\Minton...
^
SQL state: 22P02
Character: 373
Problem solved :)
pg_read_binary_file('D:\Folder\Folder\FileName_.png')::bytea,
That requires either superuser privileges or membership in the pg_read_server_files system role.

postgres - inserting a column value as literal 'null'

Using pgadmin, I am trying to import data into a table from a CSV file - as it's a lot faster than running SQL scripts.
I am having problem when one of the column value is 'NULL'. The problem is that postgres is taking it as null where as this should really be 'NULL'. How can I avoid this?
in other words, the below CSV import fails:
my_column
A1B2
NULL // Postgres fails here because it thinks I am inserting a null into non-nullable column, but it's a text value
Thanks,

ERROR: extra data after last expected column on postgres

When I tried to copy a very large txt file into my postgres database, I got a following error below.
Note that I created a table with a single column and am not using any delimiter when importing the txt file.
db1=# create table travis_2018_data (v text);
db1=# \COPY travis_2018_data FROM 'C:\Users\testu\Downloads\travis_2018\2018-Certification\PROP.txt';
The error:
ERROR: extra data after last expected column
CONTEXT: COPY travis_2018_data, line 295032: "000000561125P 02018000000000000
I'm wondering why I still get the error about the extra data (or column) on line 295032 ?
Your text probably contains a tab character which is the default column delimiter for the TEXT format when using \copy (or copy) without specifying a format.
So \copy thinks the line contains two column but only expects one, hence the error message
You need to specify a delimiter that will not occur in the file. The character with the ASCII value 1 is a highly unlikely to occur in such a file, so you can try:
\COPY travis_2018_data FROM '.....' DELIMITER E'\x01'

Postgresql CSV import not working [duplicate]

I use basketball data tables to get some understanding of Postgres 9.2 & phppgadmin. Therefore I would like to import csv tables into that database. However, I get:
ERROR: missing data for column "year"
CONTEXT: COPY coaches, line 1: ""coachid";"year";"yr_order";"firstname";"lastname";"season_win";"season_loss";"playoff_win";"playoff..."
with command:
\copy coaches FROM '/Users/Desktop/Database/NBAPostGres/DataOriginal/coaches_data.csv' DELIMITER ',' CSV;
The current table has no missings. So my questions are:
What did I wrong and if using a table with missing values?
How to import such table or handle such structure generally(also in respect to missing values)?
Data structure:
coachid year yr_order firstname lastname season_win
HAMBLFR01 204 2 Frank Hamblen 10
RUSSEJO01 1946 1 John Russell 22
I used:
varchar integer integer character character integer
You can have columns missing for the whole table. Tell COPY (or the psql wrapper \copy) to only fill selected columns by appending a column list to the table:
\copy coaches (coachid, yr_order, firstname)
FROM '/Users/.../coaches_data.csv' (FORMAT csv, HEADER, DELIMITER ',');
Missing values are filled in with column defaults. The manual:
If there are any columns in the table that are not in the column list,
COPY FROM will insert the default values for those columns.
But you cannot have values missing for just some rows. That's not possible. The text representation of NULL can be used (overruling respective column defaults).
It's all in the manual, really:
SQL-COPY
psql \copy
ERROR: missing data for column "year" CONTEXT: COPY coaches, line 1:
""coachid";"year";"yr_order";"firstname";"lastname";"season_win";"season_loss";"playoff_win";"playoff..."
This type of error is also the result of a Table-mismatch. The table you are importing the text file into either has more columns or less columns than the text file has.

PG COPY error: 'invalid input syntax for integer' when importing quoted CSV file without any integers

When trying to use the COPY command via SQL in Postgres 9.5.1 in a simple example database…
I am getting this error:
ERROR: invalid input syntax for integer: "Sally"
CONTEXT: COPY customer_, line 2, column id_: "Sally"
********** Error **********
ERROR: invalid input syntax for integer: "Sally"
SQL state: 22P02
Context: COPY customer_, line 2, column id_: "Sally"
…when importing this data in CSV (comma-separated value):
"first_name_","last_name_","phone_","email_"
"Sally","Jones","425.555.1324","s.jones#acme.com"
"Jarrod","Barkley","206.555.3454","j.barkley#example.com"
"Wendy","Melvin","415.555.2343","wendy#wendyandlisa.com"
"Lisa","Coleman","425.555.7282","lisa#wendyandlisa.com"
"Jesse","Johnson","507.555.7865","j.j#guitar.com"
"Jean-Luc","Martin","212.555.2244","jean-luc.martin#example.com"
…being imported via the following SQL executed in pgAdmin:
COPY customer_
FROM '/home/parallels/Downloads/customer_.csv'
CSV
HEADER
;
…into this table:
-- Table: public.customer_
-- DROP TABLE public.customer_;
CREATE TABLE public.customer_
(
id_ integer NOT NULL DEFAULT nextval('customer__id__seq'::regclass),
first_name_ text NOT NULL,
last_name_ text NOT NULL,
phone_ text NOT NULL DEFAULT ''::text,
email_ text NOT NULL DEFAULT ''::text,
CONSTRAINT pkey_customer_ PRIMARY KEY (id_)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.customer_
OWNER TO postgres;
COMMENT ON TABLE public.customer_
IS 'Represents a person whose pets visit our clinic.';
So it seems the first row containing the names of the columns is being processed successfully. The failure point is with the first data value in the first data line of the CSV. None of my imported data is of integer type, so the I am befuddled by the error message. The only integer is the id_ primary key, auto-incrementing SERIAL.
I did read the Question page on PG COPY error: invalid input syntax for integer. But that question did involve integer values, and the lack thereof in an empty quoted string being interpreted as a NULL. In my case here we have no integer values in the data; the only integer is the primary key SERIAL column with a DEFAULT generated value (not in the data being imported).
I also found the Question, PostgreSQL ERROR: invalid input syntax for integer. But it seems irrelevant.
Try specifying the columns . . . without the primary key:
COPY customer_ (first_name_ text, last_name_ text, phone_ text, email_ text)
FROM '/home/parallels/Downloads/customer_.csv'
CSV
HEADER
;
Without the column list, it is looking for a value for id_.
The import data file’s first row of column names are not used for mapping to the table columns. The HEADER flag merely tells Postgres to skip over that first line, as documented:
HEADER
Specifies that… on input, the first line is ignored. …
COPY table_name FROM 'C:\path\file.csv' DELIMITERS ',' CSV header;
This wasn't the OP's problem, but posting because this is one of the top results when I google the error message.
I was trying to import a .csv file with no header. Adding a header to the file and changing COPY ... CSV to COPY ... CSV HEADER in the sql command fixed the problem for me.