1. 程式人生 > >PostgreSQL學習筆記(一):安裝篇

PostgreSQL學習筆記(一):安裝篇

文章目錄

1. PostgreSQL是什麼?

PostgreSQL是一個物件關係型資料庫管理系統,支援SQL標準查詢語言。

  • 開源、免費。
  • 執行速度快,多程序模型。
  • 支援多平臺。
  • 容易做讀寫分離、負載均衡和資料水平拆分。

2. 安裝和配置

2.1 CentOS7線上安裝

主要基於CentOS7安裝PostgreSQL11版本。

線上基於yum安裝PostgreSQL。

  1. 安裝RPM源

    yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-centos11-11-2.noarch.rpm
    
  2. 安裝客戶端

    yum install postgresql11
    
  3. 安裝服務端

    yum install postgresql11-server
    
  4. 初始化並設定自動啟動

    /usr/pgsql-11/bin/postgresql-11-setup initdb
    systemctl enable postgresql-11
    systemctl start postgresql-11
    
  5. 修改使用者密碼

    PostgreSQL 安裝完成後,會建立一下‘postgres’Linux系統使用者,用於執行PostgreSQL。資料庫中也會建立一個名為’postgres’的資料庫和名為’postgres’使用者,預設密碼為自動生成,需要在系統中改一下。

    ALTER USER postgres WITH PASSWORD '123456';
    
  6. 配置遠端連線

    vi /var/lib/pgsql/11/data/postgresql.conf
    
    # 將 #listen_addresses = 'localhost'  修改為  listen_addresses='*'
    # 此處 * 也可以改為你需要訪問的客戶端IP
  7. 信任遠端連線

    vi /var/lib/pgsql/11/data/pg_hba.conf
    ## ubuntu通過apt-get安裝後目錄可能在 /etc/postgresql/11/main
    

    修改後如下所示:

    # “local” is for Unix...
    local	all		all		trust
    # IPV4 local connections
    host	all		all		127.0.0.1/32	trust
    host	all		all		xxx.xxx.xxx.xxx/32		trust
    
  8. 重啟資料庫服務

    systemctl restart postgresql-11.service
    

2.2 Ubuntu線上安裝

  1. 安裝客戶端

    sudo apt-get install postgresql
    
  2. 設定使用者postgres密碼並切換使用者

    sudo passwd postgres
    su - postgres
    
  3. 修改使用者密碼

    PostgreSQL 安裝完成後,會建立一下‘postgres’Linux系統使用者,用於執行PostgreSQL。資料庫中也會建立一個名為’postgres’的資料庫和名為’postgres’使用者,預設密碼為自動生成,需要在系統中改一下。

    psql -U postgres
    ALTER USER postgres WITH PASSWORD '123456';
    
  4. 配置遠端連線

    vi /etc/postgresql/10/main/postgresql.conf
    
    # 將 #listen_addresses = 'localhost'  修改為  listen_addresses='*'
    # 此處 * 也可以改為你需要訪問的客戶端IP
    
  5. 信任遠端連線

    vi /etc/postgresql/10/main/pg_hba.conf
    

    修改後如下所示:

    # “local” is for Unix...
    local	all		all		trust
    # IPV4 local connections
    host	all		all		127.0.0.1/32	trust
    host	all		all		xxx.xxx.xxx.xxx/32		trust
    
  6. 重啟資料庫服務

    /etc/init.d/postgresql restart