查看當前數據庫的data目錄
進入psql查看當前數據庫的data目錄
[root@localhost ~]# su - postgres
-bash-4.1$ psql
psql (8.4.18)
Type "help" for help.
postgres=# show data_directory ;
data_directory
---------------------------------
/postgres_5432
(1 row)
關閉數據庫
[root@localhost ~]# /bin/su -l postgres -c "/usr/bin/pg_ctl -D /postgres_5432/ stop"
# 如果是yum方式安裝的話可以使用service postgresql stop或systemctl stop postgresql
把當前數據庫拷貝到新位置
用rsync方式把數據庫內容拷貝到新位置
- -a 保留的權限和其他目錄屬性, 能避免未來的權限問題
- -v 提供詳細輸出,以便能夠看到進度
[root@localhost ~]# rsync -av /postgres_5432 /data/pgdata_5432
修改postgresql.conf配置文件
編輯postgresql.conf文件,找到data_directory字段,修改該字段,保存退出
如果配置文件中沒有該字段則直接加在文件末尾即可
data_directory = '/data/pgdata_5432/postgres_5432/'
啓動數據庫
[root@localhost ~]# /bin/su -l postgres -c "/usr/bin/pg_ctl -D /postgres_5432/ start"
# 如果是yum方式安裝的話可以使用service postgresql start或systemctl start postgresql
查看數據庫data目錄
[root@localhost ~]# su - postgres
-bash-4.1$ psql
psql (8.4.18)
Type "help" for help.
postgres=# show data_directory ;
data_directory
---------------------------------
/data/pgdata_5432/postgres_5432
(1 row)
刪除備份
因爲我這個數據庫特殊,數據文件和配置文件都放在同一個目錄下了,如果是標準安裝的話,上一步數據庫啓動沒問題以後可以將原數據庫的數據文件刪除了,我這裏沒有刪除
重啓
[root@localhost ~]# /bin/su -l postgres -c "/usr/bin/pg_ctl -D /postgres_5432/ stop"
[root@localhost ~]# /bin/su -l postgres -c "/usr/bin/pg_ctl -D /postgres_5432/ start"
# 如果是yum方式安裝的話可以使用service postgresql restart或systemctl restart postgresql