https://github.com/marselester/pg-time
Testing Go time.Time and PostgreSQL Timestamptz
https://github.com/marselester/pg-time
golang postgresql timestamp
Last synced: about 1 month ago
JSON representation
Testing Go time.Time and PostgreSQL Timestamptz
- Host: GitHub
- URL: https://github.com/marselester/pg-time
- Owner: marselester
- Created: 2018-05-01T14:17:38.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-07T04:29:04.000Z (about 8 years ago)
- Last Synced: 2025-04-11T01:36:42.058Z (about 1 year ago)
- Topics: golang, postgresql, timestamp
- Language: Go
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PostgreSQL Timestamptz and Go Idiosyncrasies
[Time is hard topic](https://zachholman.com/talk/utc-is-enough-for-everyone-right) ⏳
PostgreSQL converts time from the connection's time zone to UTC on storage,
and from UTC to the connection's time zone on retrieval.
However I was surprised to find that [pgx PostgreSQL driver](https://github.com/jackc/pgx)
doesn't return `time.Time` in UTC format despite configuring connection with
- `postgres://account:swordfish@localhost/account?timezone=UTC`
- `SET TIME ZONE 'UTC'`
- `SET timezone TO 'UTC'`
For example, `2009-11-10 23:00:00 +0000 UTC` is retrieved from db as `2009-11-11 06:00:00 +0700 +07`.
Though the dates are equal, the UTC format is what I expected.
I compared the behavior with [pq PostgreSQL driver](https://github.com/lib/pq/) and it returns UTC date.
```sh
$ make docker_run_postgres
$ go test ./...
```
Another caveat is `time.Time` nanosecond resolution and timestamptz microsecond resolution.
The current solution is to use [Truncate](https://github.com/lib/pq/issues/227).