Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在所有Postgres版本中获取服务器操作系统本地时间_Sql_Postgresql - Fatal编程技术网

Sql 如何在所有Postgres版本中获取服务器操作系统本地时间

Sql 如何在所有Postgres版本中获取服务器操作系统本地时间,sql,postgresql,Sql,Postgresql,如何在每个Postgres版本中获取char8格式的服务器本地时间hh:mm:ss 在9.1中,它起作用: select current_time::char(8) 返回正确的当地时间13:46:00 在9.5中,它返回3小时不同的时间: 10:46:00 选择当前时间,返回版本 "10:48:40.181735+00";"PostgreSQL 9.5.2, compiled by Visual C++ build 1800, 32-bit" 及 更新 两台服务器都使用默认的postgres

如何在每个Postgres版本中获取char8格式的服务器本地时间hh:mm:ss

在9.1中,它起作用:

select current_time::char(8)
返回正确的当地时间13:46:00

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

10:46:00

选择当前时间,返回版本

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

更新

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

在9.5 Windows 10中,它包含

#timezone = 'GMT'
#timezone_abbreviations = 'Default'     
#timezone = '(defaults to server environment setting)'
#timezone_abbreviations = 'Default'
在9.1 Debian中,它包含

#timezone = 'GMT'
#timezone_abbreviations = 'Default'     
#timezone = '(defaults to server environment setting)'
#timezone_abbreviations = 'Default'
当使用默认的postgresql.conf文件时,如何在9.5中获取服务器本地时间

看起来服务器没有使用9.5中的操作系统设置

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

询问您想要的时间:

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
请注意,该函数不接受时间类型。改用时间戳

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

$ date +%Z
BRT
在psql中:

=> \! date +%Z
BRT
如果客户端存在psql:

psql -c "\! date +%Z" --host localhost --dbname=cpn --no-password
BRT
需要一个文件来避免提供密码

如果postgresql.conf中未指定时区或未将其指定为服务器命令行选项,则服务器将尝试使用TZ环境变量的值作为默认时区。如果未定义TZ或不是PostgreSQL已知的任何时区名称,则服务器将通过检查C库函数localtime的行为来尝试确定操作系统的默认时区。默认时区被选为PostgreSQL已知时区中最接近的匹配。如果未指定,这些规则还用于选择log_时区的默认值

这意味着,如果不定义时区,服务器将尝试通过检查C库函数localtime的行为来确定操作系统的默认时区

如果postgresql.conf中未指定时区或未将其指定为服务器命令行选项,则服务器将尝试使用TZ环境变量的值作为默认时区

我认为您需要删除时区='GMT' 并将GMT替换为“默认服务器环境设置”
因为“默认服务器环境设置”未定义,因此服务器试图通过检查C库函数localtime的行为来确定操作系统的默认时区。

要获取使用定义时区计算的本地时间,请执行以下操作:

select current_time, version();
select current_time at time zone 'US/Eastern';
SELECT current_setting('TIMEZONE');

要获取使用指定时区计算的本地时间,请执行以下操作:

select current_time, version();
select current_time at time zone 'US/Eastern';
SELECT current_setting('TIMEZONE');
要获取定义的时区,请执行以下操作:

select current_time, version();
select current_time at time zone 'US/Eastern';
SELECT current_setting('TIMEZONE');
要指定要使用的时区,请执行以下操作:

set timezone='US/Eastern';

您可以看到不同的时区

您可以检查这两个版本之间的时区是否存在差异吗?我相信这会出现在我用时区信息更新的postgresql.conf文件中。使用默认postgresql.conf文件时,如何在9.5中获取服务器本地时间?使用默认postgresql.conf文件是什么意思?您不能更改postgresql.conf中的时区设置吗?我正在寻找一种方法来创建可与任何postgresql.conf设置一起使用的应用程序。设置时区='gmt-3';select current_time::char8工作,但它使用硬编码时区。如何用服务器操作系统时区替换gmt-3?这个要求有点奇怪。通常使用客户端时区。为什么是服务器?你能详细说明一下吗?这是硬编码的时区快速公交。应用程序使用来自不同时区的服务器。如何使用服务器操作系统时区?应用程序正在以非超级用户权限运行。如何强制服务器获取操作系统时区?@Andrus获取操作系统时区不需要超级用户。我以非超级用户的身份发出了上述shell调用。应用程序只能使用5432端口访问服务器。没有对服务器的SSH访问,所有其他端口都已关闭。在这种情况下如何获取服务器操作系统时间?在9.5中,内置默认值为GMT,它不会尝试确定如何始终使用服务器操作系统时间,而不会更改postgresql.conf文件?每次安装新的postgresql server时,都会要求您设置时区,如果您没有选择一个,那么正如您所说的,默认设置为9.5:GMT。文档还声明:,但这通常在postgresql.conf中被覆盖。因此,如果您没有在安装时选择时区或稍后在postgresql.conf中更改时区,则无法“自动选择”服务器安装时不询问机器的默认时区。应用程序无法控制postgres server的安装方式,也无权更改postgresql.conf。它以非超级用户权限运行。在这种情况下如何获取操作系统时区?请检查postgresql.conf文件的权限和所有者。还要确保编辑了正确的配置文件,运行sudo-upostgres/usr/local/pgsql/bin/psql并输入showconfig\u file;还要检查用于更改postgresql设置的用户是否具有足够的权限。应用程序只能使用5432端口访问服务器。没有对服务器的SSH访问,所有其他端口都已关闭。如何获得服务器操作系统时间 在这种情况下?