操作系統:Centos
下載源碼包
https://www.postgresql.org/ftp/source/v10.3/
解壓
# tar -zxvf postgresql-10.3.tar.gz
編譯安裝
# cd postgresql-10.3
指定安裝路徑
# ./configure --prefix=/usr/local/postgresql
可能出現錯誤configure: error: readline library not found
執行
# yum install readline-devel
編譯安裝
# make && make install
用戶權限
添加用戶和用戶組
# useradd postgres
# groupadd postgres
生成數據庫文件目錄
# mkdir /usr/local/postgresql/data
用戶文件訪問權限變更
# chown postgres /usr/local/postgresql/data # chgrp postgres /usr/local/postgresql/data
用戶切換
# su - postgres
數據庫初始化
$ /usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data
配置修改
配置數據庫允許訪問的IP
vi /usr/local/postgresql/data/pg_hba.conf
配置IP都可以連接數據庫(如需要所有IP都可以訪問則對應為0.0.0.0/0),此處認證類型選擇的是MD5,可參考官方文檔認證類型,選擇適合的認證方式
host all postgres 10.0.0.0/8 md5
80 81 # TYPE DATABASE USER ADDRESS METHOD 82 83 # "local" is for Unix domain socket connections only 84 local all all trust 85 # IPv4 local connections: 86 host all all 127.0.0.1/32 trust 87 # IPv6 local connections: 88 host all all ::1/128 trust 89 # Allow replication connections from localhost, by a user with the 90 # replication privilege. 91 local replication all trust 92 host replication all 127.0.0.1/32 trust 93 host replication all ::1/128 trust 94 host all postgres 0.0.0.0/0 md5
配置監聽地址、連接端口號等
$ vi /usr/local/postgresql/data/postgresql.conf
listen_addresses配置監聽地址范圍,改為*則為所有
port 默認為5432
57 # - Connection Settings - 58 59 listen_addresses = '*' # what IP address(es) to listen on; 60 # comma-separated list of addresses; 61 # defaults to 'localhost'; use '*' for all 62 # (change requires restart) 63 port = 5432 # (change requires restart)
啟動數據庫服務
$ /usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data -l /usr/local/postgresql/data/logfile start
修改密碼
$ /usr/local/postgresql/bin/psql
# \password postgres
Enter new password