ubuntu下postgreSQL安裝配置

來源:互聯網
上載者:User

標籤:

一、安裝並配置,並設定遠程登陸的使用者名稱和密碼

1、安裝postgreSQL

sudo apt-get update

sudo apt-get install postgresql-9.4

  • 在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

ubuntu下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.