查看当前数据库的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