postgresql 时区与时间函数

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:


--把时间戳转成epoch值
postgres=# select extract(epoch from now());
    date_part     
------------------
 1447898857.74524
(1 row)


--把epoch 值转换回时间戳
postgres=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' +  1447898857.74524 * INTERVAL '1 second';           
           ?column?           
------------------------------
 2015-11-19 10:07:37.74524+08
 

 postgres=# SELECT TIMESTAMP WITH TIME ZONE 'epoch' +  1447898857.74524 * INTERVAL '1 second';           
           ?column?           
------------------------------
 2015-11-19 10:07:37.74524+08

 --查看当前的时间戳
postgres=# select clock_timestamp(),current_timestamp,localtimestamp;
        clock_timestamp        |              now              |         timestamp          
-------------------------------+-------------------------------+----------------------------
 2016-02-02 17:54:15.547194+08 | 2016-02-02 17:54:15.546956+08 | 2016-02-02 17:54:15.546956
 
--时间加减
postgres=# select date '2016-02-02 10:00:00'+ interval '10 minutes'; 
      ?column?       
---------------------
 2016-02-02 00:10:00
 
 
 
--直接用sql生成随机日期时间
select '2015-5-1'::date + trunc(random()*100)::integer +' 00:22:22'::time + (trunc(random()*3600*24)||' second')::interval; 

--不同时区之间的转换
postgres=# select '2016-02-03 09:07:30.816885+08' at time zone 'pst';
          timezone          
----------------------------
 2016-02-02 17:07:30.816885
(1 row)

postgres=# select '2016-02-03 09:07:30.816885+08' at time zone 'cct';
          timezone          
----------------------------
 2016-02-03 09:07:30.816885
(1 row)
  
postgres=#  SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'cct';
      timezone       
---------------------
 2001-02-17 09:38:40

--查看系统支持的时区
 select * from pg_timezone_names ; 
 
 --时区设置参数
timezone = 'PRC'

--修改时区的方法
1. 全局参数
postgresql.conf
timezone='UTC'

2. 数据库级配置
alter database dbname set timezone='UTC';

pipeline=# select * from pg_db_role_setting ;
 setdatabase | setrole |              setconfig               
-------------+---------+--------------------------------------
       14930 |       0 | {TimeZone=UTC}

3. 用户级配置
alter role rolname set timezone='UTC';
或者
alter role all set timezone='UTC';

pipeline=# select * from pg_db_role_setting ;
 setdatabase | setrole |              setconfig               
-------------+---------+--------------------------------------
       14930 |       0 | {TimeZone=UTC}
           0 |       0 | {TimeZone=UTC}
  
 --创建随机日期时间函数       
   CREATE OR REPLACE FUNCTION rand_date_time(start_date date, end_date date) RETURNS TIMESTAMP AS  
$BODY$  
 DECLARE  
    interval_days integer;  
    random_seconds integer;  
random_dates integer;  
    random_date date;  
    random_time time;
    
BEGIN  
    interval_days := end_date - start_date;  
    random_dates:= trunc(random()*interval_days);
    random_date := start_date + random_dates; 
    random_seconds:= trunc(random()*3600*24); 
    random_time:=' 00:00:00'::time+(random_seconds || ' second')::INTERVAL;
    RETURN random_date +random_time;  
END;   
$BODY$  
LANGUAGE plpgsql;  
--生成指定时间内的随机时间
SELECT rand_date_time('2000-01-01', '2013-12-31');  


--休眠1.5秒后执行,单位秒
SELECT clock_timestamp(),pg_sleep(1.5),clock_timestamp();
--休眠5分钟,单位interval
SELECT clock_timestamp(),pg_sleep_for('5 minutes'),clock_timestamp();
--到指定时间执行,注意这些休眠时间不是完全精确的
SELECT clock_timestamp(),pg_sleep_until('today 10:00'),clock_timestamp();



相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
5月前
|
关系型数据库 PostgreSQL
Postgresql设置时区
Postgresql设置时区
|
5月前
|
关系型数据库 PostgreSQL
PostgreSQL日期时间
PostgreSQL日期时间
|
8月前
|
关系型数据库 MySQL
[MySQL]日期和时间函数(二)
[MySQL]日期和时间函数(二)
|
8月前
|
Unix 关系型数据库 MySQL
|
10月前
|
索引
MySQL-日期和时间函数
MySQL-日期和时间函数
|
10月前
|
关系型数据库 MySQL
Mysql日期和时间函数1
Mysql日期和时间函数
44 0
|
存储 关系型数据库 数据库
PostgreSQL 日期与时间类型
PostgreSQL 日期与时间类型
1231 0
|
SQL 关系型数据库 MySQL
|
Unix 关系型数据库 MySQL
【mysql】日期和时间函数
【mysql】日期和时间函数
109 0
【mysql】日期和时间函数
|
存储 SQL 关系型数据库
【mysql】日期与时间类型
【mysql】日期与时间类型
970 0
【mysql】日期与时间类型