在Linux上佈置postgres10.12

#PGSQL總目錄:/home/postgres
#PGSQL數據庫存放位置:/home/postgres/pgsql_data
#PGSQL軟件安裝位置:/home/postgres/pgsql
#PGSQL軟件版本:postgresql-10.12-1-linux-x64-binaries.tar.gz

1、第一步:將PGSQL軟件版本解壓PGSQL總目錄:
#cd /home
#mkdir postgres
#cd /home/postgres
#tar -zxvf postgresql-10.12-1-linux-x64-binaries.tar.gz

第二步:在操作系統中添加用戶
#useradd postgres
#passwd postgres

第三步:創建postgres的數據存放目錄,給postgres用戶授權目錄訪問權限
#mkdir /home/postgres/pgsql_data
#chown postgres:postgres /home/postgres/pgsql_data
#chown postgres:postgres /home/postgres/pgsql

第四步:初始化數據庫
4.1 把postgres的bin目錄加入bash_profile中
#cd /home/postgre/
#vim .bash_profile
加入:(注意:要開啓pg_ctl這條命令,就是在此處設置)
export PGDATA=/home/postgres/pgsql_data/
export PATH=/home/postgres/pgsql/bin:$PATH
在這裏插入圖片描述

4.2 初始化數據庫
進入postgres用戶下操作
#/home/postgre/pgsql/bin/initdb -D /home/postgres/pgsql_data
4.3 啓動數據庫
#/home/postgre/pgsql/bin/pg_ctl -D /home/postgres/pgsql_data / start

第五步:登陸psql修改postgres數據庫用戶密碼
#su - postgres
#alter user postgres with password ‘postgres’; //密碼已經是postgres了,而且alter命令找不到

第六步:開啓遠程登錄
root下修改
修改/home/postgres/pgsql_data目錄下的pg_hba.conf 和 postgresql.conf
#vim pg_hba.conf
在這裏插入圖片描述
在pg_hba.con增加一條規則 比如讓192.168.1.0 網段訪問
在這裏插入圖片描述

32 -> 192.168.1.1/32 表示必須是來自這個IP地址的訪問才合法;
24 -> 192.168.1.0/24 表示只要來自192.168.1.0 ~ 192.168.1.255的都合法;
16 -> 192.168.0.0/16 表示只要來自192.168.0.0 ~ 192.168.255.255的都合法;
8 -> 192.0.0.0/16 表示只要來自192.0.0.0 ~ 192.255.255.255的都合法;
0 -> 0.0.0.0/0 表示全部IP地址都合法,合法的IP地址即可

在postgresql.conf中把監聽地址改爲* 代表全部地址
#vim postgresql.conf
在這裏插入圖片描述
記得將listen_addresses前面的#去掉

然後pg_ctl restart 重啓數據庫

問題解決:
如果 切換su - postgres用戶時出現#patch-2.4:這樣的提示時,則需將/etc/skel下的.bash_profile、.bash_rc、.bash_logout這三份文件移入到/home/postgres/主目錄下,
#cd /etc/skel
#cp /etc/skel/.bash_logout /home/postgres
#cp /etc/skel/.bash_profile /home/postgres
#cp /etc/skel/.bashrc /home/postgres
然後再su – posgtgres進行查看

如果遠程無法連接到該服務器,可以查看下防火牆設置,如果是密碼錯誤但是忘記密碼了,可以將pg_hba.conf中的md5改成trust,這樣就可以無密碼登錄。連接該數據庫的工具可以用pgAdmin。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章