溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

postgresql數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法

發(fā)布時(shí)間:2021-08-20 19:59:37 來源:億速云 閱讀:2518 作者:chen 欄目:數(shù)據(jù)庫

本篇內(nèi)容介紹了“postgresql數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

一:postgresql數(shù)據(jù)庫的安裝:兩種方法1,安裝包安裝。2,yum安裝

1.yum安裝。

結(jié)合自己的操作系統(tǒng)下載postgresql鏡像。之后會(huì)顯示如何使用yum安裝和啟動(dòng)postgresql,如下圖:

postgresql數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法

  • 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)中選擇對應(yīng)的os系統(tǒng)和你想要的postgresql版本,然后點(diǎn)擊下載企業(yè)版:

https://www.enterprisedb.com/download-postgresql-binaries

postgresql數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法

之后選擇需要的版本

postgresql數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法

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ù)庫庫文件存儲(chǔ)目錄、給postgres賦予權(quán)限:

mkdir /usr/local/pgsql/data

cd /usr/local/pgsql

chown postgres.postgres data

8、編輯~/.bash_profile文件

#vi ~/.bash_profile

設(shè)置以下的環(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

二:啟動(dòng),登錄,退出

如果需要指定環(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ù)自己的實(shí)際目錄指定。

編輯完環(huán)境變量文件后,運(yùn)行如下命令使環(huán)境變量生效:

postgres@lgr-pc:~$ . .bash_profile

設(shè)置完環(huán)境變量,運(yùn)行如下命令啟動(dòng)服務(wù)器

postgres@lgr-pc:~$ pg_ctl start

二 登錄服務(wù)器

當(dāng)安裝完數(shù)據(jù)庫后,我們會(huì)有一個(gè)系統(tǒng)用戶,一個(gè)數(shù)據(jù)庫,一個(gè)數(shù)據(jù)庫用戶,他們默認(rèn)的名稱為:postgres

1. 如果沒有設(shè)置bin目錄的環(huán)境變量,那么

postgres@lgr-pc:~$ /usr/local/pgsql/bin/psql

這樣默認(rèn)登錄到postgres庫中,當(dāng)然也可以在psql后面加上庫的名稱,這樣就可以登錄到指定庫中。如登錄到test庫:

postgres@lgr-pc:~$ /usr/local/pgsql/bin/psql test

如果您也像我一樣設(shè)置了bin目錄的環(huán)境變量,那么

postgres@lgr-pc:~$ psql

這樣默認(rèn)的也是登錄到postgres庫中,同樣的我們可以指定數(shù)據(jù)庫名稱,登錄到指定庫。

postgres@lgr-pc:~$ psql test

三 退出登錄

退出登錄就很簡單了,我們可以運(yùn)行\(zhòng)q,或者ctrl+d

postgres=# \q

四 關(guān)閉數(shù)據(jù)庫服務(wù)器

關(guān)閉:

postgres@lgr-pc:~$ pg_ctl stop

重啟:

postgres@lgr-pc:~$ pg_ctl restart

三:PGSQL更改數(shù)據(jù)的存儲(chǔ)路徑:

1)在數(shù)據(jù)庫軟件安裝之后,初始化數(shù)據(jù)庫時(shí)候,可以指定初始化時(shí)創(chuàng)建的數(shù)據(jù)庫的默認(rèn)文件路徑,

指定數(shù)據(jù)庫存放位置和編碼方式,初始化數(shù)據(jù)庫:

/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data

這樣初始化之后,再修改配置文件postgresql.conf為修改之后的數(shù)據(jù)文件路徑,就能保證以后的數(shù)據(jù)文件也在這個(gè)路徑下了。

2)如果是已經(jīng)初始化好了,再修改數(shù)據(jù)文件路徑的,如下過程:

找到配置文件查看原來的數(shù)據(jù)存儲(chǔ)路徑在哪

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/

設(shè)置用戶和權(quán)限

sudo chown -R postgres:postgres /data/postgresql/

sudo chmod 700 /data/postgresql/

將配置文件的數(shù)據(jù)存儲(chǔ)路徑改成新的

sudo vim /etc/postgresql/9.6/main/postgresql.conf

data_directory=‘/data/postgresql/datafile’

再啟動(dòng)就行了

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”查看當(dāng)前數(shù)據(jù)目錄

postgres=# show data_directory;

data_directory

------------------------

/var/lib/pgsql/10/data

(1 row)

“postgresql數(shù)據(jù)庫的安裝以及修改數(shù)據(jù)文件路徑的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI