postgresql常用命令

來源:互聯網
上載者:User

標籤:

原文地址:http://blog.chinaunix.net/uid-26642180-id-3485465.html  

最近一直在學習Postgresql,下面是自己整理的Postgresql的常用命令

串連資料庫, 預設的使用者和資料庫是postgres
psql -U user -d dbname

切換資料庫,相當於mysql的use dbname
\c dbname
列舉資料庫,相當於mysql的show databases
\l
列舉表,相當於mysql的show tables
\dt
查看錶結構,相當於desc tblname,show columns from tbname
\d tblname

\di 查看索引 

建立資料庫: 
create database [資料庫名]; 
刪除資料庫: 
drop database [資料庫名];  
*重新命名一個表: 
alter table [表名A] rename to [表名B]; 
*刪除一個表: 
drop table [表名]; 

*在已有的表裡添加欄位: 
alter table [表名] add column [欄位名] [類型]; 
*刪除表中的欄位: 
alter table [表名] drop column [欄位名]; 
*重新命名一個欄位:  
alter table [表名] rename column [欄位名A] to [欄位名B]; 
*給一個欄位設定預設值:  
alter table [表名] alter column [欄位名] set default [新的預設值];
*去除預設值:  
alter table [表名] alter column [欄位名] drop default; 
在表中插入資料: 
insert into 表名 ([欄位名m],[欄位名n],......) values ([列m的值],[列n的值],......); 
修改表中的某行某列的資料: 
update [表名] set [目標欄位名]=[目標值] where [該行特徵]; 
刪除表中某行資料: 
delete from [表名] where [該行特徵]; 
delete from [表名];--刪空整個表 
建立表: 
create table ([欄位名1] [類型1] ;,[欄位名2] [類型2],......<,primary key (欄位名m,欄位名n,...)>;); 

\copyright     顯示 PostgreSQL 的使用和發行條款
\encoding [字元編碼名稱]
                 顯示或設定使用者端字元編碼
\h [名稱]      SQL 命令文法上的說明,用 * 顯示全部命令
\prompt [文本] 名稱
                 提示使用者設定內部變數
\password [USERNAME]
                 securely change the password for a user
\q             退出 psql

 

可以使用pg_dump和pg_dumpall來完成。比如備份sales資料庫: 
pg_dump drupal>/opt/Postgresql/backup/1.bak 

postgresql常用命令

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.