共計 3070 個字符,預計需要花費 8 分鐘才能閱讀完成。
本篇內(nèi)容介紹了“postgresql 數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓丸趣 TV 小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
一:postgresql 數(shù)據(jù)庫的安裝:兩種方法 1,安裝包安裝。2,yum 安裝
1.yum 安裝。
結合自己的操作系統(tǒng)下載 postgresql 鏡像。之后會顯示如何使用 yum 安裝和啟動 postgresql,如下圖:
Install the repository RPM:
yum installhttps://download.postgresql.org/pub/repos/yum/10/redhat/rhel-6-x86_64/pgdg-centos10-10-1.noarch.rpm
Install the client packages:
yum install postgresql10
Optionally install the server packages:
yum install postgresql10-server
Optionally initialize the database and enable automatic start:
service postgresql-10 initdb
chkconfig postgresql-10 on
service postgresql-10 start
2). 使用鏡像安裝。
1、下載 postgresql 最新版:http://www.postgresql.org/ftp/source/
或者在官網(wǎng)中選擇對應的 os 系統(tǒng)和你想要的 postgresql 版本,然后點擊下載企業(yè)版:
https://www.enterprisedb.com/download-postgresql-binaries
之后選擇需要的版本
2、解壓文件:
tar zxvf postgresql-8.3.7.tar.gz
cd postgresql-8.3.7
3、編譯,指定安裝 postgresql 的路徑
./configure –prefix=/usr/local/pgsql
4、編譯:
make
5、安裝:
make install
6、創(chuàng)建用戶組和用戶:
groupadd postgres
useradd -g postgres postgres
7、創(chuàng)建數(shù)據(jù)庫庫文件存儲目錄、給 postgres 賦予權限:
mkdir /usr/local/pgsql/data
cd /usr/local/pgsql
chown postgres.postgres data
8、編輯~/.bash_profile 文件
#vi ~/.bash_profile
設置以下的環(huán)境變量
export PGHOME=/usr/local/pgsql
export PGDATA=/usr/local/pgsql/data
9、初始化數(shù)據(jù)庫目錄:
切換用戶
su – postgresql
初始化數(shù)據(jù) - D 指定初始化創(chuàng)建的數(shù)據(jù)庫的文件路徑
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
二:啟動,登錄,退出
如果需要指定環(huán)境變量,請按如下方式:
postgres@lgr-pc:~$ vi .bash_profile
添加如下內(nèi)容:
export PGDATA=/usr/local/pgsql/data;
export PATH=$PATH:$HOME/bin:/usr/local/pgsql/bin
即指定 pgdata 和 bin 的目錄,這里可以根據(jù)自己的實際目錄指定。
編輯完環(huán)境變量文件后,運行如下命令使環(huán)境變量生效:
postgres@lgr-pc:~$ . .bash_profile
設置完環(huán)境變量,運行如下命令啟動服務器:
postgres@lgr-pc:~$ pg_ctl start
二 登錄服務器
當安裝完數(shù)據(jù)庫后,我們會有一個系統(tǒng)用戶,一個數(shù)據(jù)庫,一個數(shù)據(jù)庫用戶,他們默認的名稱為:postgres
1. 如果沒有設置 bin 目錄的環(huán)境變量,那么
postgres@lgr-pc:~$ /usr/local/pgsql/bin/psql
這樣默認登錄到 postgres 庫中,當然也可以在 psql 后面加上庫的名稱,這樣就可以登錄到指定庫中。如登錄到 test 庫:
postgres@lgr-pc:~$ /usr/local/pgsql/bin/psql test
如果您也像我一樣設置了 bin 目錄的環(huán)境變量,那么
postgres@lgr-pc:~$ psql
這樣默認的也是登錄到 postgres 庫中,同樣的我們可以指定數(shù)據(jù)庫名稱,登錄到指定庫。
postgres@lgr-pc:~$ psql test
三 退出登錄
退出登錄就很簡單了,我們可以運行 \q,或者 ctrl+d
postgres=# \q
四 關閉數(shù)據(jù)庫服務器
關閉:
postgres@lgr-pc:~$ pg_ctl stop
重啟:
postgres@lgr-pc:~$ pg_ctl restart
三:PGSQL 更改數(shù)據(jù)的存儲路徑:
1)在數(shù)據(jù)庫軟件安裝之后,初始化數(shù)據(jù)庫時候,可以指定初始化時創(chuàng)建的數(shù)據(jù)庫的默認文件路徑,
指定數(shù)據(jù)庫存放位置和編碼方式,初始化數(shù)據(jù)庫:
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
這樣初始化之后,再修改配置文件 postgresql.conf 為修改之后的數(shù)據(jù)文件路徑,就能保證以后的數(shù)據(jù)文件也在這個路徑下了。
2)如果是已經(jīng)初始化好了,再修改數(shù)據(jù)文件路徑的,如下過程:
找到配置文件查看原來的數(shù)據(jù)存儲路徑在哪
sudo find / -name postgresql.conf
一般是在 /etc/postgresql/9.6/main/postgresql.conf
停掉 PGSQL
sudo service postgresql stop
拷貝原來的數(shù)據(jù)路徑到新的路徑下
sudo cp -rf /var/lib/postgresql/9.6/main/ /data/postgresql/
設置用戶和權限
sudo chown -R postgres:postgres /data/postgresql/
sudo chmod 700 /data/postgresql/
將配置文件的數(shù)據(jù)存儲路徑改成新的
sudo vim /etc/postgresql/9.6/main/postgresql.conf
data_directory=‘/data/postgresql/datafile’
再啟動就行了
sudo service postgresql start
快速找到 配置文件中 data_directory 所在的行數(shù):
bogon:root@/usr/pgsql-10/bin cat /var/lib/pgsql/10/data/postgresql.conf | grep -n data_directory
41:#data_directory = ConfigDir # use data in another directory
修改完畢后,可以用 psql 命令“show data_directory”查看當前數(shù)據(jù)目錄
postgres=# show data_directory;
data_directory
————————
/var/lib/pgsql/10/data
(1 row)
“postgresql 數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注丸趣 TV 網(wǎng)站,丸趣 TV 小編將為大家輸出更多高質(zhì)量的實用文章!