字段基本型別
int
char(num)
varchar(num)
serial
更多詳情 限制
not null
primary key
unique
登入
psql -u dbuser 預設登入進入名為dbuser的資料庫中,如果不存在請-d指定相應進入的資料庫
psql -u username -p port -h localhost -d dbname
命令列
建立資料庫使用者具有超級許可權
createuser dbuser -s ;
createdb dbname ;
password dbuser ;
psql 進入postgres
基本命令
\l 顯示當前所有的資料庫
\c dbname 切換進入資料庫
\c 檢視當前的連線資訊
\d 當前庫的所有表資訊
\d tablename 當前表的所有字段資訊
\q 退出登入
建立使用者
create user dbuser ;
password dbuser ;
create user dbuser with password 'pwd' ;
create database dbname owner dbuser ;
授權
grant all privileges on database dbname to dbuser ;
建立資料庫
create database dbname ;
drop database dbname ;
備份資料
pg_dump -h host -u user dbname > file.sql.bak
還原資料
psql -h host -u user -d dbname < file.sql.bak
建立新錶
//serial 指定自增
create table users(id serial primary key,name varchar(20) not null,info text,lasttime date,age int not null default 23) ;
插入資料
insert into user_tbl(name, signup_date) values(';張三';, ';2013-12-22';);
選擇記錄
select * from tablename limit length offset start ;
更新資料
update user_tbl set name = '李四' where name = '張三' ;
刪除記錄
delete from user_tbl where name = '李四' ;
清空表資料
truncate table tset1
delete from tset1
新增字段
alter table user_tbl add email varchar(40);
更新結構
alter table user_tbl alter column signup_date set not null;
alter table gdt_accounts alter system_industry_id type varchar(12) ;
更名字段
alter table user_tbl rename column signup_date to signup;
刪除字段
alter table user_tbl drop column email;
**更名
alter table user_tbl rename to backup_tbl;
刪除**
drop table if exists backup_tbl;
sql優化
postgresql獲取資料庫中所有table表名
select tablename from pg_tables
where tablename not like 'pg%'
and tablename not like 'sql_%'
order by tablename;
postgresql獲取某個表tablename所有欄位名稱 , 型別,備註,是否為空 等
select a.attname as name, pg_type.typname as typename, col_description(a.attrelid,a.attnum) as comment,a.attnotnull as notnull
from pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid
where c.relname = 'tablename' and a.attrelid = c.oid and a.attnum>0
postgresql獲取某個表tablename的主鍵資訊
select pg_attribute.attname as colname,pg_type.typname as typename,pg_constraint.conname as pk_name from
pg_constraint inner join pg_class
on pg_constraint.conrelid = pg_class.oid
inner join pg_attribute on pg_attribute.attrelid = pg_class.oid
and pg_attribute.attnum = pg_constraint.conkey[1]
inner join pg_type on pg_type.oid = pg_attribute.atttypid
where pg_class.relname = 'tablename'
and pg_constraint.contype='p'
postgresql獲取資料庫中所有view名檢視
select viewname from pg_views
where schemaname ='public'
新增注釋
postgresql給列新增注釋資訊
create table user( userid int not null, phonenumber int); comment on column user.userid is 'the user id';
postgresql給表新增注釋資訊
comment on table user is 'our session logs';
postgresql 查詢注釋資訊
select description from pg_descriptionjoin pg_class on pg_description.objoid = pg_class.oidwhere relname = 'user'
查詢指定schema下注釋資訊
select description from pg_description join pg_class on pg_description.objoid = pg_class.oid join pg_namespace on pg_class.relnamespace = pg_namespace.oid where relname = '' and nspname=''
新增索引
建立索引
create index indexname on test1 (column);
分析優化
explain analyze select 1 from tbl_user where user_spell like '%cyl%';
django框架學習十六 postgreSQL
window下安裝 2.安裝psycopg2 postgresql介面卡 pip install psycopg2 2.7.4建立使用者及資料庫 1.win r r然後cmd進入doc視窗 2.進入到postgresql的bin檔案下 3.初始化資料庫 4.啟動資料庫 5.建立blog使用者並輸入口...
postgres遞迴查詢
1 開發中我們經常會遇到資料庫中的資料存在存在上小級的父子關係,如全國的省市資訊,或者乙個公司的部門資訊等。如果我們在查詢的時候使用遞迴的方法直接將資料查詢出來就可以避免我們在程式中在進行遞迴過濾資料了,下面我們以全國的省市為例,首先我們定義一張表,表的定義如下 然後我們在其中放一些資料,資料如下 ...
修改postgres編碼
連線資料庫是控制台出現亂碼 修改 postgresql.conf檔案的 lc messages utf8 locale for system error message postgresq資料庫為了安全,它不會監聽除本地以外的所有連線請求,當使用者通過jdbc訪問是,會報一些如下的異常 org.po...