examples of storing timestamps in postgres?

4,288 views
Skip to first unread message

Christopher Hobbs

unread,
Aug 14, 2012, 5:21:59 PM8/14/12
to golan...@googlegroups.com
I'm using Postgres and I've got a column that's a timestamp with timezone and I'm using pq.  Does anyone have examples of how to store a timestamp in Postgres with Go?  So far all of my db interaction has simply been strings.

Thanks!

Jeff Hodges

unread,
Aug 15, 2012, 12:14:07 AM8/15/12
to Christopher Hobbs, golan...@googlegroups.com
Just pass in a time.Time object as the parameter in your insert! See
<https://github.com/bmizerany/pq/blob/master/encode.go#L25>

Example:

db.Exec("insert into faketimetable (my_time) values (($1));", time.Now())
Message has been deleted

Jeff Hodges

unread,
Aug 15, 2012, 9:25:05 PM8/15/12
to Christopher Hobbs, golan...@googlegroups.com
Exec() is variadic and takes `interface{}`s , so it can take more as
you add more. So, `db.Exec("insert into faketable (name, created_at)
VALUES (($1), (2));", "myname", time.Now())` would work.

Yeah, there is a dearth of documentation in pq and the database/sql
package docs seem to leave the task of documentation up the
implementing packages. Which is maybe not great but there's decent
reasoning for that. Each database has its own escaping style, so
people might use the wrong one if there's some "canonical" form
documented in database/sql.

This could probably be solved by some nice examples in a doc.go file in pq.

On Wed, Aug 15, 2012 at 1:36 PM, Christopher Hobbs <ch...@altbit.org> wrote:
> How can this be extended for multiple attributes? Can you mix attribute
> types (string, timestamp, etc)?

Jeff Hodges

unread,
Aug 15, 2012, 9:25:30 PM8/15/12
to Christopher Hobbs, golan...@googlegroups.com
Sigh, that (2) should be ($2), of course.
Reply all
Reply to author
Forward
0 new messages