I have a table in Oracle with timestamp data in "JAN-16-15 05.10.14.034000000 PM".
When I created the table in postgresql with "col_name timestamp" it is showing data as "2015-01-16 17:10:14.034".
Any suggestions on how can set the column to get the data format as in postgre same to what I have in Oracle?
Timestamps (or dates or numbers) do not have any "format2. Neither in Postgres nor in Oracle or in any other relational database).
Any "format" you see, is applied by your SQL client displaying those values.
You need to configure your SQL client to use a different format for timestamp, or use the to_char() function to format the value as you want.
In particular, to get the format you desire, use
SELECT to_char(current_timestamp, 'MON-MM-YY HH.MI.SS.US000 AM');
The output format can be changed in psql by changing the DateStyle parameter, but I would strongly recommend to not change it away from the default ISO format as that also affects the input that is parsed.
Related
I'm trying to bulk import some data into Postgres using COPY but I can't get my timestamp to insert in bulk.
Reduced example:
DROP TABLE IF EXISTS "demo";
CREATE TABLE "demo"(
time TIMESTAMP WITH TIME ZONE NOT NULL
);
I have a CSV file which looks like this:
1609459247.579
I try and insert my data using COPY:
COPY public.demo FROM '/home/myuser/demo.csv' WITH (FORMAT csv)
I get:
invalid input syntax for type timestamp with time zone: "1609459247.579"
If I want to insert the data manually I need to use to_timestamp.
INSERT INTO public.demo VALUES(to_timestamp(1609459247.579))
Is there either a way I can get COPY to use to_timestamp at insert time, or some pre-processing I can do to the CSV file so that copy will insert it? It's very slow doing it one INSERT at a time.
Thanks!
Have solved this. I used a COPY TO from my table to create me a csv the opposite way round with a format I know Postgres doesn't mind, then mucked about with Python string formatting to come up with a format that works.
Short version, in Python you can use a datetime formatted with datetime.isoformat() to create something Postgres will ingest using COPY.
I created all my Postgres tables using knex timestampz and now I get this error when trying to query anything. One possible solution is to convert all the columns to timestamp without timezone, but could I keep timezone and solve this error with photon?
The following SQL Query:
CREATE TABLE "SomeTable" ("dateEnd" DATE)
Creates a table SomeTable with a column dateEnd. However, the database-type is Timestamp, not Date. It used to work, but after reimporting a whole database dump, all the Date data-types are replaced by Timestamp data-types. Even If I create a very simple table, like the one above, the data-type jumps to Timestamp. I am using DB2 express c version 11.1.0.
If your Db2 database was created in Oracle Compatibility mode, then DATE columns are implemented as TIMESTAMP(0) columns to match what Oracle does.
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.apdv.porting.doc/doc/r0053667.html
https://www.ibm.com/support/knowledgecenter/SSEPGG_11.1.0/com.ibm.db2.luw.admin.config.doc/doc/r0054912.html
BTW you may want to use either Db2 Developer-C or Db2 Developer Community Edition. Those are effectively replacing the old Express-C edition
https://www.ibm.com/uk-en/marketplace/ibm-db2-direct-and-developer-editions
I setup a postgres db that was installed on a server in the Central Time Zone so all the timestamp columns are in Central Time Zone.
Is there a way in postgres to change all the timezone columns in a database from CST to GMT? Is it a best practice to configure databases to use GMT?
Well, best practice is to avoid using TIMESTAMP type which does not know anything about timezones and to always use TIMESTAMPTZ (short for TIMESTAMP WITH TIME ZONE) type to store your timestamps.
TIMESTAMPTZ stores both timestamp in UTC and timezone in which timestamp was originally written to (like CST, PST or GMT+6). This allows you to always manipulate and display these columns correctly, no matter what current server or client timezone setting is.
You should be able to convert your existing TIMESTAMP columns into TIMESTAMPTZ using something like:
ALTER TABLE mytable ALTER COLUMN old_tstamp TYPE TIMESTAMPTZ
Before doing this, you should experiment on small dataset or maybe small test table on how conversion from TIMESTAMP to TIMESTAMPTZ is really working for you such that time zone information is preserved on your data.
If conversion does not work correctly, you can temporarily set timezone for current session (you need it only for conversion purposes) using statement like (use it before ALTER TABLE ... COLUMN ... TYPE):
SET timezone TO 'CST6CDT';
or
SET timezone TO 'UTC+6';
This will affect subsequent operations and conversions from TIMESTAMP to TIMESTAMPTZ - just make sure to get it right.
After you have converted all timestamps to TIMESTAMPTZ, server or client timezone setting (which defaults to current operating system timezone setting) is only useful for display purposes, as data manipulation will be always correct.
I am mainly from a Sql Server background, and following some issues with getting MySql to work with the Microsoft Sync Framework (namely it does not cater for snapshots), I am having to look into Postgres and try to get that working with the Sync Framework.
The triggers that are needed include a call to function "##DBTS", but I am having trouble finding an equivalent in Postgres for this.
From the microsoft documentation for this it says:
##DBTS returns the current database's last-used timestamp value.
A new timestamp value is generated when a row with a timestamp
column is inserted or updated.
In MySql it was the following:
USE INFORMATION_SCHEMA;
SELECT MAX(UPDATE_TIME) FROM TABLES WHERE UPDATE_TIME < NOW();
Can anyone tell me what this would be in Postgres?
PostgreSQL does not keep track when a table was last modified. So there is no equivalent for SQL Server's ##DBTS nor for MySQL's INFORMATION_SCHEMA.TABLES.UPDATE_TIME.
You also might be interested in this discussion:
http://archives.postgresql.org/pgsql-general/2009-02/msg01171.php
which essentially says: "if you need to know when a table was last modified, you have to add a timestamp column to each table that records that last time the row was updated".