前端之家收集整理的这篇文章主要介绍了
postgresql 常用操作命令,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
create database exampleDB with owner exampleowner encoding 'UTF8'; //创建库
create user exampleowner superuser password 'example'; //创建超级角色
/usr/local/pgsql/bin/psql -U postgres -c "select * from pg_stat_activity where procpid=18210" //查看一个PG进程在干什么
SELECT count(*) from pg_stat_activity; //查看当前总连接数
SELECT count(*) FROM (SELECT pg_stat_get_backend_idset() AS backendid) AS s;
ALTER DATABASE name RENAME TO newname; //数据库改名
ALTER DATABASE example_db RENAME TO example_db_bak;
alter table t_provider drop Column agree; //删除表字段
grant all on database example_db to backend; //授予用户全部权限在这个数据库
grant select on table to user; //用户授权,授予user用户对表table 的select权限
alter user postgres with password 'new password'; //更改密码
alter user pinganowner login; //给用户login权限
update pg_database set datname = 'newName' where datname = 'oldName'; //把数据库名oldName改为newName。
ALTER DATABASE name OWNER TO new_owner; //修改数据库所属人
alter table exampleTable owner to user; //修改表所属人
create user exampleUser with password 'example'; //创建带密码用户
select pg_size_pretty(pg_database_size('db_name')); //统计数据库大小
select indexrelname,pg_size_pretty(pg_relation_size(indexrelname))
from pg_stat_user_indexes
where schemaname = 'public'
order by pg_relation_size(indexrelname) desc; \\查看所有索引的大小。
select relname,pg_size_pretty(pg_relation_size(relname))
from pg_stat_user_tables
where schemaname = 'public'
order by pg_relation_size(relname) desc; 查所有表的大小。
select pg_size_pretty(pg_relation_size('table_name')); \\查看单个表的大小。
alter table t_provider alter company_scale type integer using company_scale::integer; \\强制类型转换
(select (random()* 200 )::integer 生成随机数字
select columns1,columns2,columns3 into tmp_example from exampleTable; 创建临时表into后边是new_table
COPY (select * from pg_stat_activity) TO '/home/postgres/p.csv' CSV HEADER; 导出当前进程为csv格式的文件
原文链接:https://www.f2er.com/postgresql/196098.html