Postgresql 基礎

postgresql 運程訪問

pg_hba.conf -> host  all    all    192.168.0.0/24    md5
postgresql.conf -> listen_addresses = '*' # what IP address(es) to listen on;

postgresql 連線被拒,請檢查主機名稱和埠號,並確定 postmaster 可以接受 TCP/IP 連線

這主要是由於用戶密碼認證方式引起的,Postgresql數據庫安裝好後默認採用md5密碼加密認證方式。
pg_hba.conf -> host all all    0.0.0.0/0    md5
postgresql.conf -> listen_addresses = '*' # what IP address(es) to listen on;
安裝jdbc驅動 (程序 -> PostgreSQL -> Application Stack Builder -> Database Driders(全部安裝))
重啓服務start server

PostgreSQL 替換字符串方法及字符串操作函數

update ab set a=replace(a,'aaa','0') 把a字段裏面的‘aaa’字符串替換成0
[其它相關函數](http://www.jsjtt.com/shujuku/postgresql/29.html)

 

單表distinct/多表group by查詢去除重複記錄

select count(distinct member_id) from abc_coupongain where coupon_id='Coupon_0000000000000000000000542' group by member_id

 

Column 'id' in field list is ambiguous

列'ID'在字段列表中重複,其實就是兩張表有相同的字段,但是使用時表字段的名稱前沒有加表名,導致指代不明

轉發排行前50名

SELECT
  count(1) AS logCount,
  info.headimgurl AS headimgurl,
  info.mobile AS NAME,
  info.openid AS openid
FROM
  wcd_log log
INNER JOIN user_info info ON log.belong_id = info.openid
WHERE
  wcd_id ='2314' group by belong_id order by logCount desc limit 50

修改數據庫密碼

alter user postgres with password 'new password';

查詢某個字段中是否包含字符串 

select count(*) as 使用次數, abc_log.name as 功能名稱 from abc_log where abc_log.type <>'04' and position('Enterp_' in name)=0 group by abc_log.name   order by 使用次數 desc  LIMIT 100

mysql用 locate('Enterp_', name);

 

查詢數據庫連接數情況

select * from pg_stat_activity order by query_start desc

pg 創建函數

CREATE OR REPLACE FUNCTION createSeqId(text, text)
RETURNS text AS
$BODY$
DECLARE
t text;
i int;
j int;
n int;
BEGIN
n=nextval($2);
i=32-character_length($1)-character_length(n||'');
j=1;
t='';
for j IN 1..i loop
T = T||'0';
END loop ;
t = $1 || t||n;
RETURN t;
END;
$BODY$
LANGUAGE 'plpgsql' VOLATILE;

postgresql 字符串轉化爲數字類型排序

SELECT sell FROM abc_product WHERE enterprise_id = '' ORDER BY to_number(sell, '999999999') desc

postgresql null 排序問題

SELECT sell FROM abc_product WHERE enterprise_id = ''ORDER BY (sell IS not NULL), sell asc

postgresql 去除重複數據

select * from abc_news where  news_id  in (select min(news_id) from abc_news where category='Category_00000000000000000344331' group by sort) order by sort desc

windows my.init文件位置

C:\ProgramData\MySQL\MySQL Server 5.5 

查看並修改 max_allowed_packet 大小

show VARIABLES like '%max_allowed_packet%';
set global max_allowed_packet = 20*1024*1024*10;
show VARIABLES like '%max_allowed_packet%';

關聯標籤高級搜索

select * from tag_relation_ship where type='01' and tag_id in (55,64,82) group by item_id  having count(item_id) > 2

數據還原

// 直接在終端輸入,無需進入psql
psql -h localhost -p 5432 -U postgres -d jihui88_backup -t abc_category < /Users/wangyj/Downloads/abbcc2.dmp

計算表數據佔用量

SELECT
	table_schema,
	TABLE_NAME,
	reltuples,
	pg_size_pretty (
		pg_total_relation_size (
			'"' || table_schema || '"."' || TABLE_NAME || '"'
		)
	)
FROM
	pg_class,
	information_schema.tables
WHERE
	relname = TABLE_NAME
ORDER BY
	reltuples DESC
LIMIT 20

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章