1枯跑、安裝PostgreSQL,當(dāng)前版本是9.3
sudo apt-get install -y postgresql
2罢屈、修改數(shù)據(jù)庫文件的保存路徑(也可不調(diào)使鹅,默認(rèn)路徑為:/var/lib/postgresql/9.3/main/base)
注意數(shù)據(jù)庫版本,當(dāng)前安裝的是9.3版本遣总。新建數(shù)據(jù)庫存儲目錄為/home/postgresql/data
mkdir -p /home/postgresql/data #新的保存數(shù)據(jù)庫文件的目錄
cd /home/postgresql
chown -R postgres:postgres /home/postgresql
chmod -R 700 /home/postgresql
psql --version #查看數(shù)據(jù)庫版本
su postgres #進(jìn)入postgres用戶
/usr/lib/postgresql/9.3/bin/initdb -D /home/postgresql/data #生成新的數(shù)據(jù)庫路徑睬罗,9.3為數(shù)據(jù)庫版本
exit
修改配置文件
/etc/init.d/postgresql stop #停止數(shù)據(jù)庫服務(wù)(或者用:service postgresql stop)
vi /etc/postgresql/9.3/main/postgresql.conf #打開配置文件
#data_directory = '/var/lib/postgresql/9.3/main' 改為 data_directory = '/home/postgresql/data'
#保存配置文件
/etc/init.d/postgresql start #啟動數(shù)據(jù)庫服務(wù)(或者用:service postgresql start)
3、修改PostgreSQL數(shù)據(jù)庫的默認(rèn)用戶postgres的密碼(注意不是linux系統(tǒng)帳號)
sudo -u postgres psql
ALTER USER postgres WITH PASSWORD 'postgres'; #修改PostgreSQL的登錄密碼為:postgres
\q #退出PostgreSQL psql客戶端
其中sudo -u postgres
是使用 postgres 用戶登錄的意思旭斥,PostgreSQL 數(shù)據(jù)默認(rèn)會創(chuàng)建一個 postgres 的數(shù)據(jù)庫用戶作為數(shù)據(jù)庫的管理員容达,密碼是隨機的,所以這里把密碼設(shè)定為postgres
垂券。
4花盐、修改linux系統(tǒng)的postgres用戶的密碼(密碼與數(shù)據(jù)庫用戶postgres的密碼相同)
sudo passwd -d postgres #刪除PostgreSQL用戶密碼
sudo -u postgres passwd #輸入兩邊新密碼:postgres
passwd -d
是清空指定用戶密碼的意思,PostgreSQL 數(shù)據(jù)默認(rèn)會創(chuàng)建一個 linux 用戶 postgres菇爪,通過上面的代碼修改密碼為postgres
算芯。
5、修改PostgresSQL數(shù)據(jù)庫配置實現(xiàn)遠(yuǎn)程訪問
1凳宙、打開配置文件熙揍,修改以下內(nèi)容
vi /etc/postgresql/9.3/main/postgresql.conf
2、監(jiān)聽任何地址訪問氏涩,修改連接權(quán)限
#listen_addresses = 'localhost' 改為 listen_addresses = '*'
3届囚、啟用密碼驗證
#password_encryption = on 改為 password_encryption = on
4、保存配置文件是尖,退出vi編輯器
1意系、打開配置文件,增加以下內(nèi)容饺汹,配置可訪問的用戶ip段
vi /etc/postgresql/9.1/main/pg_hba.conf
2蛔添、在文檔末尾加上以下內(nèi)容
host all all 0.0.0.0 0.0.0.0 md5
3、保存配置文件,退出vi編輯器
重啟PostgreSQL數(shù)據(jù)庫
/etc/init.d/postgresql restart
6作郭、配置5432端口的防火墻設(shè)置
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
7、內(nèi)部登錄弦疮,管理數(shù)據(jù)庫夹攒、新建數(shù)據(jù)庫、用戶和密碼
1胁塞、登錄PostgreSQL數(shù)據(jù)庫
psql -U postgres -h 127.0.0.1
2咏尝、創(chuàng)建新用戶zhangps,但不給建數(shù)據(jù)庫的權(quán)限啸罢,注意用戶名要用雙引號编检,以區(qū)分大小寫,密碼不用
postgres=# create user "zhangps" with password '123456' nocreatedb;
3扰才、建立數(shù)據(jù)庫允懂,并指定所有者
postgres=# create database "testdb" with owner = "zhangps";
4、退出
\q
8衩匣、外部登錄蕾总,管理數(shù)據(jù)庫、新建數(shù)據(jù)庫琅捏、用戶和密碼
1.在外部命令行的管理命令生百,創(chuàng)建用戶pencil
sudo -u postgres createuser -D -P pencil
輸入新的密碼:
再次輸入新的密碼:
2.建立數(shù)據(jù)庫(tempdb),并指定所有者為(pencil)
sudo -u postgres createdb -O pencil tempdb
-O
設(shè)定所有者為pencil
9柄延、安裝 PostgreSQL 數(shù)據(jù)庫 pgAdmin3 客戶端管理程序
apt-get install -y pgadmin3
在圖形界面下才可以使用蚀浆,Ubuntu的圖形界面啟動pgadmin,只需要按下鍵盤的windows鍵搜吧,在搜索中輸入pgadmin市俊,就可以查找到它,點擊就可以啟動滤奈。如果要方便以后使用秕衙,可以把它拖到啟動器上鎖定就行了。