列出数据库:psql -l
退出psql:\q
使用psql登入:psql -U gpadmin tutorial
切换数据库:\c tutorial
显示表结构:\d faa.sample
执行sql文件:\i create_table.sql
打开执行时间显示:\timing on
查看、kill会话
select * from pg_stat_activity;
select pg_terminate_backend(procpid) from pg_stat_activity where usename='xxx';
查看用户连接数:
select * from get_pg_stats() where usename='query_for_smartbi'
表
--创建表
create table public.people_record
(id int primary key,
record varchar(20) not null
)distributed by(id);
create table public.people_record1 (like public.people_record)
distributed randomly;
--查找表
select * from pg_tables where tablename like '%people%';
字段
-- 修改列名、字段
alter table public.report3_part2 rename name to badge;
alter table public.report1_part2 alter column col1 type int;
-- using语法,指定alter时转换的方式,这里是先将"6.0"转为float,再转为integer
alter table public.report1_part2 alter column col1 type int using col1::float;
视图
-- 获取视图定义语句
select pg_get_viewdef('pg_partitions',true);
权限
--查询用户xz是否有表的select权限
select has_table_privilege('xz','public.LC_InstiArchive','select');
values用法
values(1,2),(2,3); -- 单独使用时不用加表名列名
select b,a from (values(1,2),(2,3)) t(a,b); --from时需包上括号并加别名
时间
select
'2019-01-01'::timestamp,
timestamp '2019-01-01 12:12:12',
date '2019-01-01',
now()+interval '1 days 2 hours 2 minutes 2 seconds',
to_char(now(),'yyyy-mm-dd hh24:mi:ss'),
;
--两个时间段是否有交叉
select (date'20190101',interval '30 days')overlaps(date'2019-02-01',date'2019-02-21');
窗口函数:类似于 hive的窗口函数 ,注意没有rows between用法,只有row_number(pv) OVER(PARTITION BY cookieid ORDER BY createtime desc)。
copy
-- 使用copy命令导入数据
copy faa.sample from '/tutorials/2.csv' with csv delimiter as ',';
--使用copy命令导出数据
copy faa.sample to '/tutorials/3.csv' with csv delimiter as '\t' escape as '\n';
python,只有管理员才能创建语言和python函数
create language plpythonu;
create or replace function public.pythontime()
returns text as
$$
import time
return time.time()
$$language plpythonu;
问题排查解决
- 聚合时内存不足:1、拆分查询;2、set enable_hashagg=off; 改用group aggregate