Insert data to postgres table - postgresql

I'm Trying to insert the data from csv file which was exported from Oracle DB. when I try to import on PGadmin. its failing with below error.
ERROR: invalid input syntax for type timestamp: "29-APR-18
12.04.07.000000000 AM" CONTEXT: COPY consolidated_dtls_job_log, line 1, column start_time: "29-APR-18 12.04.07.000000000 AM"
Note: Column Start_time is created with timestamp datatype.

Use a different NLS_TIMESTAMP_TZ_FORMAT when exporting the data from Oracle; something that is closer to the ISO format.
Here is an SQL statement provided by Belayer:
ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH:MI:SS.FF TZH:TZM';

Related

Postgres error when parsing date with timezone

I have a table with a bunch of records, with different values for a date and I need them all parsed as a date value, so I'm trying to parse a date in postgres and I'm receiving an error which doesn't tell me much
select to_Date(:original_date, 'YYYYmmDD');
When I pass this value to original_date is when I get the error: '2022-11-18T11:02:08-03:00'
Here's the error I'm getting:
SQL Error [22008]: ERROR: date/time field value out of range: "2022-11-18T11:02:08-03:00"
Where: SQL statement "select to_Date(original_date, 'YYYYmmDD')"
PL/pgSQL function parse_date(character varying) line 5 at SQL statement
As mentioned by Hambone in the comment below the question, changing my date format to 'YYYY-mm-DD' works like a charm.
Thanks for that Hambone!

Postgres timestamp formatting in query

I am migrating a code with its own ORM from Db2 to postgres. I have a query that executes the following sql on postgres 10 -
SELECT * FROM TriggerQueue
WHERE TriggerQueue.atServerStartup = 'Y'
AND (TriggerQueue.scheduledatetime > '2018-06-21 20.02.57.827' OR
TriggerQueue.scheduleDateTime is null) AND TriggerQueue.inputQueue = 'N'
but the pgadmin is showing the following error:
ERROR: invalid input syntax for type timestamp: "2018-06-21
20.02.57.827"
LINE 3: AND (TriggerQueue.scheduledatetime > '2018-06-21 20.02.57.8...
^
SQL state: 22007
I'm guessing the timestamp format is wrong based on sql state, but I'm not sure how to format the value. Any insight on this would be very helpful.
EDIT : Timestamp field in pg is of the type timestamp without timezone. Pgadmin shows size of 6.
Use a proper timestamp literal:
timestamp '2018-06-21 20:02:57.827'
Note the : to separate hours, minutes and seconds

Setting date format parameter on a sqoop-import job

I am having trouble casting a date column to a string using sqoop-import from an oracle database to an HDFS parquet file. I am using the following:
sqoop-import -Doraoop.oracle.session.initialization.statements="alter session set nls_date_format='YYYYMMDD'"
My understanding is that this should execute the above statement before it begins transferring data. I have also tried
-Duser.nls_date_format="YYYYMMDD"
But this doesn't work either, the resulting parquet file still contains the original date format as listed in the table. If it matters, I am running these in a bash script and also casting the same date columns to string using --map-column-java "MY_DATE_COL_NAME=String"What am I doing wrong?
Thanks very much.
Source: SqoopUserGuide
Oracle JDBC represents DATE and TIME SQL types as TIMESTAMP values. Any DATE columns in an Oracle database will be imported as a TIMESTAMP in Sqoop, and Sqoop-generated code will store these values in java.sql.Timestamp fields.
You can try casting date to String while importing within the query.
For Example
sqoop import -- query 'select col1, col2, ..., TO_CHAR(MY_DATE_COL_NAME, 'YYYY-MM-DD') FROM TableName WHERE $CONDITIONS'

Postgres: Error when using COPY from a CSV with timestamptz type

I am using Postgres 9.5.3(On Ubuntu 16.04) and I have a table with some timestamptz fields
...
datetime_received timestamptz NULL,
datetime_manufactured timestamptz NULL,
...
I used the following SQL command to generate CSV file:
COPY (select * from tmp_table limit 100000) TO '/tmp/aa.csv' DELIMITER ';' CSV HEADER;
and used:
COPY tmp_table FROM '/tmp/aa.csv' DELIMITER ';' CSV ENCODING 'UTF-8';
to import into the table.
The example of rows in the CSV file:
CM0030;;INV_AVAILABLE;2016-07-30 14:50:42.141+07;;2016-08-06 00:00:000+07;FAHCM00001;;123;;;;;1.000000;1.000000;;;;;;;;80000.000000;;;2016-07-30 14:59:08.959+07;2016-07-30 14:59:08.959+07;2016-07-30 14:59:08.959+07;2016-07-30 14:59:08.959+07;
But I encounter the following error when running the second command:
ERROR: invalid input syntax for type timestamp with time zone: "datetime_received"
CONTEXT: COPY inventory_item, line 1, column datetime_received: "datetime_received"
My database's timezone is:
show timezone;
TimeZone
-----------
localtime(GMT+7)
(1 row)
Is there any missing step or wrong configuration?
Any suggestions are appreciated!
The error you're seeing means that Postgres is trying (and failing) to convert the string 'datetime_received' to a timestamp value.
This is happening because COPY is trying to insert the header row into your table. You need to include a HEADER clause on the COPY FROM command, just like you did for the COPY TO.
More generally, when using COPY to move data around, you should make sure that the TO and FROM commands are using exactly the same options. Specifying ENCODING for one command and not the other can lead to errors, or silently corrupt data, if your client encoding is not UTF8.

How to insert current datetime in postgresql insert query [duplicate]

This question already has answers here:
in postgres, can you set the default formatting for a timestamp, by session or globally?
(3 answers)
Closed 6 years ago.
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
This is the query I have used for mysql to insert current date time.
When I am using this in postgresql, I am getting below error.
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
********** Error **********
ERROR: function utc_timestamp() does not exist
SQL state: 42883
I have tried like below using now(), however it is inserting like "2016-07-07 17:01:18.410677". I need to insert in 'yyyymmdd hh:mi:ss tt' format.
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
How to insert current date time in insert query of postgresql in above format ?
timestamp (or date or time columns) do NOT have "a format".
Any formatting you see is applied by the SQL client you are using.
To insert the current time use current_timestamp as documented in the manual:
INSERT into "Group" (name,createddate)
VALUES ('Test', current_timestamp);
To display that value in a different format change the configuration of your SQL client or format the value when SELECTing the data:
select name, to_char(createddate, 'yyyymmdd hh:mi:ss tt') as created_date
from "Group"
For psql (the default command line client) you can configure the display format through the configuration parameter DateStyle: https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE
For current datetime, you can use now() function in postgresql insert query.
You can also refer following link.
insert statement in postgres for data type timestamp without time zone NOT NULL,.
You can of course format the result of current_timestamp().
Please have a look at the various formatting functions in the official documentation.