ubuntu下postgreSQL安裝配置
一、安裝並配置,並設置遠程登陸的用戶名和密碼
1、安裝postgreSQL
sudo apt-get update
sudo apt-get install postgresql-10 -y
- 在Ubuntu下安裝Postgresql后,會自動注冊為服務,並隨操作系統自動啟動。
- 在Ubuntu下安裝Postgresql后,會自動添加一個名為postgres的操作系統用戶,密碼是隨機的。並且會自動生成一個名字為postgres的數據庫,用戶名也為postgres,密碼也是隨機的。
2、修改postgres數據庫用戶的密碼為123456
打開客戶端工具(psql)
sudo -u postgres psql
- 其中,sudo -u postgres 是使用postgres 用戶登錄的意思
- PostgreSQL數據默認會創建一個postgres的數據庫用戶作為數據庫的管理員,密碼是隨機的
postgres=# ALTER USER postgres WITH PASSWORD '123456';
- postgres=#為PostgreSQL下的命令提示符,--注意最后的分號;
3、退出PostgreSQL psql客戶端
postgres=# \q
4、修改ubuntu操作系統的postgres用戶的密碼(密碼要與數據庫用戶postgres的密碼相同)
切換到root用戶
su root
刪除PostgreSQL用戶密碼
sudo passwd -d postgres
- passwd -d 是清空指定用戶密碼的意思
設置PostgreSQL系統用戶的密碼
sudo -u postgres passwd
按照提示,輸入兩次新密碼
- 輸入新的 UNIX 密碼
- 重新輸入新的 UNIX 密碼
- passwd:已成功更新密碼
5、修改PostgresSQL數據庫配置實現遠程訪問
vi /etc/postgresql/9.4/main/postgresql.conf
1.監聽任何地址訪問,修改連接權限
#listen_addresses = 'localhost' 改為 listen_addresses = '*'
2.啟用密碼驗證
#password_encryption = on 改為 password_encryption = on
vi /etc/postgresql/9.4/main/pg_hba.conf
在文檔末尾加上以下內容
host all all 0.0.0.0 0.0.0.0 md5
6、重啟服務
/etc/init.d/postgresql restart
7、5432端口的防火牆設置
5432為postgreSQL默認的端口
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 5432 -j ACCEPT
二、內部登錄,管理數據庫、新建數據庫、用戶和密碼
1、登錄postgre SQL數據庫
psql -U postgres -h 127.0.0.1
2、創建新用戶zhangps,但不給建數據庫的權限
postgres=# create user "zhangps" with password '123456' nocreatedb;
- 用戶名處是雙引號
3、建立數據庫,並指定所有者
postgres=#create database "testdb" with owner = "zhangps";
三、外部登錄,管理數據庫、新建數據庫、用戶和密碼
1、在外部命令行的管理命令,創建用戶pencil
sudo -u postgres createuser -D -P pencil
- 輸入新的密碼:
- 再次輸入新的密碼:
2、建立數據庫(tempdb),並指定所有者為(pencil)
sudo -u postgres createdb -O pencil tempdb
- -O設定所有者為pencil
參考:http://blog.sina.com.cn/s/blog_6af33caa0100ypck.html