programing

postgresql 삽입 쿼리에 현재 날짜 시간을 삽입하는 방법

bestprogram 2023. 5. 27. 12:03

postgresql 삽입 쿼리에 현재 날짜 시간을 삽입하는 방법

INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);

현재 날짜를 삽입하기 위해 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

아래와 같이 사용해 보았습니다.now()그러나 그것은 처럼 삽입됩니다."2016-07-07 17:01:18.410677"삽입해야 합니다.'yyyymmdd hh:mi:ss tt'서식을 정하다

INSERT into Group (Name,CreatedDate) VALUES ('Test',UTC_TIMESTAMP(), 1);

위 형식으로 postgresql의 삽입 쿼리에 현재 날짜 시간을 삽입하는 방법은 무엇입니까?

timestamp(또는)date또는time열)에는 "형식"이 없습니다.

표시되는 모든 형식은 사용 중인 SQL 클라이언트에 의해 적용됩니다.


현재 시간을 삽입하려면 사용current_timestamp 설명서에 설명된 대로:

INSERT into "Group" (name,createddate) 
VALUES ('Test', current_timestamp);

해당 값을 다른 형식으로 표시하려면 데이터를 선택할 때 SQL 클라이언트의 구성을 변경하거나 값을 형식으로 지정합니다.

select name, to_char(createddate, 'yyyymmdd hh:mi:ss tt') as created_date
from "Group"

위해서psql(기본 명령줄 클라이언트) 구성 매개 변수를 통해 표시 형식을 구성할 수 있습니다.DateStyle: https://www.postgresql.org/docs/current/static/runtime-config-client.html#GUC-DATESTYLE

현재 날짜 시간의 경우 postgresql 삽입 쿼리에서 now() 함수를 사용할 수 있습니다.

다음 링크를 참조할 수도 있습니다.

시간대가 NULL이 아닌 데이터 형식 타임스탬프의 postgres에 문을 삽입합니다.

물론 다음과 같은 결과를 형식화할 수 있습니다.current_timestamp()공식 문서의 다양한 포맷 기능을 살펴보시기 바랍니다.

언급URL : https://stackoverflow.com/questions/38245025/how-to-insert-current-datetime-in-postgresql-insert-query