postgresql 如何在postgresql插入查询中插入当前日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38245025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert current datetime in postgresql insert query
提问by Shesha
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.
这是我用于 mysql 插入当前日期时间的查询。当我在 postgresql 中使用它时,出现以下错误。
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.
我已经尝试过如下使用now()
,但是它插入的是"2016-07-07 17:01:18.410677"
。我需要插入'yyyymmdd hh:mi:ss tt'
格式。
INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);
How to insert current date time in insert query of postgresql in above format ?
如何以上述格式在 postgresql 的插入查询中插入当前日期时间?
回答by a_horse_with_no_name
timestamp
(or date
or time
columns) do NOThave "a format".
timestamp
(或date
或time
列)没有“格式”。
Any formatting you see is applied by the SQL client you are using.
您看到的任何格式都由您使用的 SQL 客户端应用。
To insert the current time use current_timestamp
as documented in the manual:
要current_timestamp
按照手册中的说明插入当前时间使用:
INSERT into "Group" (name,createddate)
VALUES ('Test', current_timestamp);
To displaythat value in a different format change the configuration of your SQL client or format the value when SELECTing the data:
要以不同的格式显示该值,请在选择数据时更改 SQL 客户端的配置或格式化该值:
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
对于psql
(默认命令行客户端)可以通过配置参数配置显示格式DateStyle
:https: //www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE
回答by Krutika Patel
For current datetime, you can use now() function in postgresql insert query.
对于当前日期时间,您可以在 postgresql 插入查询中使用 now() 函数。
You can also refer following link.
您也可以参考以下链接。
insert statement in postgres for data type timestamp without time zone NOT NULL,.
回答by nidhoeggr09
You can of course format the result of current_timestamp()
.
Please have a look at the various formatting functions in the official documentation.
您当然可以格式化current_timestamp()
. 请查看官方文档中的各种格式化功能。