041.PGSQL-pgsql时间日期错误问题,原来是系统时间问题,使用远程的NTP时间服务器来提供时间的话,需要使用timedatectl设置将NTP时间同步开启。

 

pgsql时间错误问题

 

 

mydb@[local]:5432=#select now();
now
-------------------------------
2021-07-05 08:07:22.981157+08


mydb@[local]:5432=# show time zone;
TimeZone
---------------
Asia/Shanghai
(1 row)

 

查看可供选择的时区

复制代码
mydb@[local]:5432=#select * from pg_timezone_names;
                  name                  | abbrev | utc_offset | is_dst
----------------------------------------+--------+------------+--------
 Africa/Accra                           | GMT    | 00:00:00   | f
 Africa/Algiers                         | CET    | 01:00:00   | f
 Africa/Bissau                          | GMT    | 00:00:00   | f
 Africa/Casablanca                      | WEST   | 01:00:00   | t
 Africa/Ceuta                           | CEST   | 02:00:00   | t
 Africa/El_Aaiun                        | WEST   | 01:00:00   | t
 Africa/Monrovia                        | GMT    | 00:00:00   | f
 Africa/Ndjamena                        | WAT    | 01:00:00   | f
 Africa/Tunis                           | CET    | 01:00:00   | f
 Africa/Windhoek                        | WAT    | 01:00:00   | f
 Africa/Abidjan                         | GMT    | 00:00:00   | f
 Africa/Bamako                          | GMT    | 00:00:00   | f
 Africa/Banjul                          | GMT    | 00:00:00   | f
 Africa/Conakry                         | GMT    | 00:00:00   | f

复制代码

中国公用时间  (PRC指:People's Republic of China)

mydb@[local]:5432=#select * from pg_timezone_names where name = 'PRC';
name | abbrev | utc_offset | is_dst
------+--------+------------+--------
PRC | CST | 08:00:00 | f
(1 row)

 

修改配置文件

[root@s101 /var/lib/pgsql/13/data]#nano postgresql.conf
log_timezone = 'PRC'

timezone = 'PRC'

重启数据库

[root@s101 /var/lib/pgsql/13/data]#sudo systemctl restart postgresql-13

再次查看还是没变

 

原来是系统时间错误

 

 设置系统时间

https://blog.csdn.net/weixin_43545225/article/details/106118354

 

https://www.cnblogs.com/actively/p/14078643.html

再次查看

 

 

 

 

----------------- 续篇

最近发现时间又不对,硬件时间是对的

date

hwclock --show

timedatectl

[root@s101 /var/lib/pgsql/13/data]#date

[root@s101 /var/lib/pgsql/13/data]#hwclock --show

[root@s101 /var/lib/pgsql/13/data]#timedatectl

 

使用远程的NTP时间服务器来提供时间的话,需要使用timedatectl设置将NTP时间同步开启。 

 开启ntp

[root@s101 /usr/share/zoneinfo/Asia]#timedatectl set-ntp yes

重启时间模块系统

[root@s101 /usr/share/zoneinfo/Asia]#systemctl restart systemd-timedated.service

同步硬件时间等于系统时间

[root@s101 /usr/share/zoneinfo/Asia]#/sbin/hwclock --systohc

 

最终版

 ntpdate time.nist.gov

修改crontab 定时调度   每小时同步一次时间

 

同步时间成功后调整硬件时间

#hwclock -w

复制代码
    1.编写调度文件
        [/etc/crontab]
        SHELL=/bin/bash
        PATH=/sbin:/bin:/usr/sbin:/usr/bin
        MAILTO=root

        # For details see man 4 crontabs

        # Example of job definition:
        # .---------------- minute (0 - 59),分钟
        # |  .------------- hour (0 - 23)
        # |  |  .---------- day of month (1 - 31)
        # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
        # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
        # |  |  |  |  |
        # *  *  *  *  * user-name  command to be executed
        * */01 * * * root ntpdate time.nist.gov;hwclock -w2.启动调度服务
        $>su root
        $>service crond status        //查看服务状态
        $>service crond start        //启动服务器

    3.设置crond服务开机自启
        $>chkconfig crond on        //启用开机自启
复制代码

 

crontab设置参考

https://www.cnblogs.com/intval/p/5763929.html

 

linux系统时间设置更新同步

https://www.cnblogs.com/whymoney1000/p/11059803.html

 

参考文档

https://blog.csdn.net/solaraceboy/article/details/78831319

 https://www.cnblogs.com/abclife/p/6171323.html

posted @   star521  阅读(925)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
历史上的今天:
2020-07-28 007.PGSQL-python读取txt文件,将数据转化为dataFrame,dataFrame数据插入到pgsql; dataframe去掉索引,指定列为索引;python读取pgsql数据,读取数据库表导成excel
点击右上角即可分享
微信分享提示