Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 458
Shesha Avatar asked Jul 07 '16 12:07

Shesha


People also ask

How do I insert current date in Pgadmin?

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,.

How do I get the current date and time in PostgreSQL?

Postgresql now() The NOW() function in Postgresql is used to get the current date and time. The return type of the NOW() function is the timestamp with the time zone. We can fetch the current date and time by using the PostgreSQL NOW() function. This function has a return type i.e. the timestamp with the time zone.


1 Answers

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

like image 137
a_horse_with_no_name Avatar answered Dec 03 '22 10:12

a_horse_with_no_name