postgresql 小盒子
------------------------------------------------------------------------------------------查看postgresql 連接情況: select * from pg_stat_activity;
------------------------------------------------------------------------------------------
timestamp to unix
select user_id, date_part('epoch',create_time)::integer from tb_account;
------------------------------------------------------------------------------------------
unix to timestamp------------------------------------------------------------------------------------------
select user_id, to_timestamp(online_time) as online from login_log order by user_id;
select to_timestamp(online_time)::timestamp without time zone as online_t from login_log;
select to_timestamp(online_time)::timestamp without time zone as online_t from login_log;
------------------------------------------------------------------------------------------
列出所有數據庫
\list
------------------------------------------------------------------------------------------
查看當前連接,當前的操作
select * from pg_stat_activity;
------------------------------------------------------------------------------------------
psql 顯示查詢時間
\timing
------------------------------------------------------------------------------------------
查詢所有的索引,所有的表
select * from pg_stat_user_indexes;
select * from pg_stat_user_tables;
select * from pg_stat_user_tables;
-------------------------------------------------------------------------------------------
client 編碼
1 使用psql 里的 \encoding 命令。 \encoding 允許你動態修改客戶端編碼。 比如,\encoding utf8
2 使用 libpq 函數。 \encoding 在做此用途的時候實際上是調用 PQsetClientEncoding()。
int PQsetClientEncoding(PGconn *conn, const char *encoding);這里 conn 與后端的聯接,而 encoding 是你想用的編碼。如果編碼設置成功它返回 0,否則返回 -1。本次聯接的當前編碼可以用下面函數顯示:
int PQclientEncoding(const PGconn *conn);請注意它只返回編碼 ID,而不是象 EUC_JP 這樣的編碼符號字串。 要把編碼 ID 轉換為編碼符號,你可以用:
char *pg_encoding_to_char(int encoding_id);
3 使用 SET client_encoding TO 。 可以用 SQL 命令設置客戶端編碼:
SET CLIENT_ENCODING TO 'value';你還可以把 SQL 語法里的 SET NAMES用于這個目的:
SET NAMES 'value';查詢當前客戶端編碼:
SHOW client_encoding;返回缺省編碼:
RESET client_encoding;
4 使用 PGCLIENTENCODING。 如果在客戶端的環境里定義了 PGCLIENTENCODING 環境變量, 那么在與服務器進行聯接時將自動選擇客戶端編碼。 (這個編碼隨后可以用上面談到的任何其它方法覆蓋。)
5 使用client_encoding配置變量。 如果在 postgresql.conf 里設置了 client_encoding 變量, 那么在與服務器建立了聯接之后,這個客戶端編碼將自動選定。(這個設置隨后可以被上面提到 的其他方法覆蓋。)
--------------------------------------------------------------------------------------------無需手動輸入密碼
1 可以設置pg_hba.conf
2 可以在用戶目錄下新建 .pgpass文件, 內容為ip:port:dbname:username:password 例如 localhost:5432:*:postgres:321654
3 可以通過設置PGPASSWORD 環境變量
--------------------------------------------------------------------------------------------
刪除所有表
1 創建函數
CREATE FUNCTION drop_all_table() RETURNS void AS $$
DECLARE
tmp VARCHAR(512);
DECLARE names CURSOR FOR
select tablename from pg_tables where schemaname='public';
BEGIN
FOR stmt IN names LOOP
tmp := 'DROP TABLE '|| quote_ident(stmt.tablename) || ' CASCADE;';
RAISE NOTICE 'notice: %', tmp;
EXECUTE 'DROP TABLE '|| quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
RAISE NOTICE 'finished .....';
END;
$$ LANGUAGE plpgsql;
2 執行 select drop_all_table();
----------------------------------------------------------------------------------------------
加注釋, 給表結構加注釋
COMMENT ON COLUMN table.col IS 'this is comment';
postgresql 只能通加comment加注釋, 不能在create語句里加
----------------------------------------------------------------------------------------------
DECLARE
tmp VARCHAR(512);
DECLARE names CURSOR FOR
select tablename from pg_tables where schemaname='public';
BEGIN
FOR stmt IN names LOOP
tmp := 'DROP TABLE '|| quote_ident(stmt.tablename) || ' CASCADE;';
RAISE NOTICE 'notice: %', tmp;
EXECUTE 'DROP TABLE '|| quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
RAISE NOTICE 'finished .....';
END;
$$ LANGUAGE plpgsql;
2 執行 select drop_all_table();
----------------------------------------------------------------------------------------------
加注釋, 給表結構加注釋
COMMENT ON COLUMN table.col IS 'this is comment';
postgresql 只能通加comment加注釋, 不能在create語句里加
----------------------------------------------------------------------------------------------
posted on 2013-07-23 17:45 多彩人生 閱讀(382) 評論(0) 編輯 收藏 引用 所屬分類: postgresql