Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store Golang time.time in Postgresql timestamp?

Tags:

postgresql

go

May I know how to store the time.time object in Postgresql?

For example, the SQL query:

INSERT INTO "UserAccount" ("email", "login_time") VALUES ('[email protected]', 2017-12-12 00:58:26.9589451 +0800 +08 m=+1406.914186601)

I tried to use loginTime := time.Now(), and it gives a time format that Postgresql don't really understand, e.g. 2017-12-12 00:58:26.9589451 +0800 +08 m=+1406.914186601

But if I try to use loginTime := time.Now().Format(time.RFC3339), and the compiler complain that loginTime is a string, and I need it to be time.time type.

May I know how to handle this?

Sorry for asking newbie question, new to both Golang and Postgres. :)

like image 746
Boo Yan Jiong Avatar asked Dec 11 '17 17:12

Boo Yan Jiong


1 Answers

The pq driver (which I assume you are using) automatically converts time.Time instances correctly for you.

so, you can do:

db.Exec(`INSERT INTO "UserAccount" ("email", "login_time") VALUES ($1, $2)`,"[email protected]",time.Now())
like image 187
David Budworth Avatar answered Sep 28 '22 10:09

David Budworth