如何在所有Postgres版本中获取服务器操作系统的本地时间 [英] How to get server OS local time in all Postgres versions

查看:147
本文介绍了如何在所有Postgres版本中获取服务器操作系统的本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在每个Postgres版本中以char(8)格式hh:mm:ss获取服务器本地时间。

How to get server local time in char(8) format hh:mm:ss in every Postgres version.

在9.1中,它起作用:

In 9.1 it works:

select current_time::char(8)

返回适当的本地时间13:46:00

returns proper local time 13:46:00

在9.5中,它返回3小时的不同时间:

In 9.5 it returns 3 hour different time:

10:46:00

选择current_time,version()返回

select current_time, version() returns

"10:48:40.181735+00";"PostgreSQL 9.5.2, compiled by Visual C++ build 1800, 32-bit"

"13:48:51.775138+03";"PostgreSQL 9.1.2 on x86_64-unknown-linux-gnu, compiled by gcc-4.4.real (Debian 4.4.5-8) 4.4.5, 64-bit"

更新

两个服务器都使用默认的postgres.conf设置作为时间。
postgres.conf不包含时区设置。

Both servers use default postgres.conf settings for time. postgres.conf does not contain timezone settings.

在9.5 Windows 10中包含

in 9.5 Windows 10 it contains

#timezone = 'GMT'
#timezone_abbreviations = 'Default'     

在9.1 Debian中包含

in 9.1 Debian it contains

#timezone = '(defaults to server environment setting)'
#timezone_abbreviations = 'Default'

在使用默认postgresql.conf文件时,如何在9.5中获取服务器本地时间?

How to get server local time in 9.5 when default postgresql.conf file is used ?

服务器似乎没有在9.5中使用操作系统设置

It looks like server does not use OS setting in 9.5

如何强制9.5从操作系统中询问时区并返回该区域的时间?

How to force 9.5 to ask timezone from OS and return time in this zone?

推荐答案

询问 时区 您想要的:

Ask for the time zone you want:

select current_time at time zone 'brt';
      timezone      
--------------------
 08:26:16.778448-03

如果需要字符串:

select to_char(current_timestamp at time zone 'brt', 'HH24:MI:SS');
 to_char  
----------
 08:32:07

请注意 to_char 函数不接受 time 类型。改用 timestamp

从shell获取操作系统本地时区。在Linux中:

Get the OS local time zone from the shell. In Linux:

$ date +%Z
BRT

在psql中:

=> \! date +%Z
BRT

如果客户端有psql:

If there is psql at the client:

psql -c "\! date +%Z" --host localhost --dbname=cpn --no-password
BRT

必须 .pgpass 文件,以避免提供密码。

It is necessary a .pgpass file to avoid supplying the password.

这篇关于如何在所有Postgres版本中获取服务器操作系统的本地时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆