下載安裝
- windows版本
https://www.postgresql.org/download/ - Ubuntu版本 安裝文檔(選擇適合的版本惨奕,14.04為例)
https://www.postgresql.org/download/linux/ubuntu/
1.創(chuàng)建文件
/etc/apt/sources.list.d/pgdg.list
2.在文件中添加以下行
deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main
3.依次執(zhí)行
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
sudo apt-key add -
sudo apt-get update
apt-get install postgresql-9.6
修改數(shù)據(jù)庫默認管理員賬號的密碼
sudo -u postgres psql
postgres=# alter user postgres with password '123456';
刪除管理員密碼命令:sudo -u postgres psql -d postgres
配置數(shù)據(jù)庫以允許遠程連接訪問
- 修改監(jiān)聽地址
sudo vim /etc/postgresql/9.6/main/postgresql.conf
將 #listen_addresses = 'localhost' 的注釋去掉并改為 listen_addresses = '*'
- 修改可訪問用戶的IP段
sudo gedit /etc/postgresql/9.6/main/pg_hba.conf
在文件末尾添加:host all all 192.168.31.0/24 md5
其中:192.168.31.0/24 表示 192.168.31.0~ 192.168.31.255的地址均可訪問,24表示非固定(32表示固定地址)
- 重啟數(shù)據(jù)庫
sudo /etc/init.d/postgresql restart
添加新用戶和新數(shù)據(jù)庫
運行系統(tǒng)用戶"postgres"的psql命令竭钝,進入客戶端:
sudo -u postgres psql
創(chuàng)建用戶"tom"并設置密碼:
postgres=# create user tom with password '123456';
創(chuàng)建數(shù)據(jù)庫practicedb梨撞,所有者tom:
postgres=# create database practicedb owner tom;
也可以使用權(quán)限操作將數(shù)據(jù)庫的所有權(quán)限賦予用戶
grant all privileges on database practicedb to tom;
PostgreSQL基本操作
通過 sudo -u postgres psql
進入,提示符變成: postgres=#
\password:設置密碼
\q:退出
\h:查看SQL命令的解釋香罐,比如\h select卧波。
\?:查看psql命令列表。
\l:列出所有數(shù)據(jù)庫庇茫。
\c [database_name]:連接其他數(shù)據(jù)庫港粱。
\d:列出當前數(shù)據(jù)庫的所有表格。
\d [table_name]:列出某一張表格的結(jié)構(gòu)港令。
\du:列出所有用戶啥容。
\e:打開文本編輯器。
\conninfo:列出當前數(shù)據(jù)庫和連接的信息顷霹。
幾個常用的操作
\du 查看數(shù)據(jù)庫賬戶
\c [dbname]: 切換其它數(shù)據(jù)庫
\d 列出當前數(shù)據(jù)庫的所有表格
\d [表名]:列出指定表結(jié)構(gòu)