Question

I'm trying to execute a stored procedure in postgres using .net and Npgsql to return now() value.

Running from .net

strSQL = "select aa_realtime2();"
cmd = New NpgsqlCommand(strSQL, connRead)
strSQL = cmd.ExecuteScalar()

I get "2014-04-07 22:07:33.015+02"

But if I run from server

select aa_realtime2();

I get "2014-04-07 15:38:11.734-04:30"

My local time is gmt -4:30 so the server is ok.

Postgres and .net app are on the same pc.

Was it helpful?

Solution

To me both values look like a very similar time just given in different time zones.

I'd like to quote from http://www.depesz.com/2014/04/04/how-to-deal-with-timestamps/ because it discusses a similar issue.

[…] Well, whenever dealing with timestamptz values, if the timezone is not specified – PostgreSQL uses configured timezone.

And you can configure it in multiple ways:

timezone “GUC" in postgresql.conf
alter database … set timezone = ‘…'
alter user … set timezone = ‘…'
SET timezone = ‘…'

First one is used to specify in which timezone your server is. Or – the default timezone that will be used unless something else changes the default.

The next two change defaults for given database and user.

The last one can be used if you want your db connection to work under different setting. […]

Is it possible that your database connections simply use different time zones?

OTHER TIPS

Most client drivers like PgJDBC, nPgSQL, etc, will work in UTC, and expect you to convert to/from UTC within your application. So they'll set the TimeZone session variable to UTC.

By contrast, psql by default leaves TimeZone at the server default, which is the timezone of the operating system.

If you want a local time from a query in .net, I recommend using the AT TIME ZONE operator to explicitly convert the time zone. Or fetch the date in UTC and do the timezone conversion with .net client-side code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top