PostgreSQL的DB在表空间之间迁移

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: BackgroundThe  /data/01 disk space is insufficient, but /data/02 is sufficient, so we migrate some data to /data/02.

Background

The  /data/01 disk space is insufficient, but /data/02 is sufficient, so we migrate some data to /data/02.


1.Backup DB and upload to s3

pg_dump --verbose -Fc --dbname=region_il | gzip > /data/02/backup/region_il_20180907.psql.gz

pg_dump --verbose -Fc --dbname=region_anz | gzip > /data/02/backup/region_anz_20180907.psql.gz

pg_dump --verbose -Fc --dbname=region_mea | gzip > /data/02/backup/region_mea_20180907.psql.gz

pg_dump --verbose -Fc --dbname=region_sa | gzip > /data/02/backup/region_sa_20180907.psql.gz



$ aws s3 cp region_il_20180907.psql.gz s3://dba-backups/

$ aws s3 cp region_anz_20180907.psql.gz s3://dba-backups/

$ aws s3 cp region_mea_20180907.psql.gz s3://dba-backups/

$ aws s3 cp region_sa_20180907.psql.gz s3://dba-backups/


$ aws s3 ls s3://dba-backups/ |grep "20180907.psql.gz"

2018-07-09 07:31:57 1831857418 region_anz_20180907.psql.gz

2018-07-09 07:33:57 1615345844 region_il_20180907.psql.gz

2018-07-09 07:37:05 8780321291 region_mea_20180907.psql.gz

2018-07-09 07:44:52 20429541766 region_sa_20180907.psql.gz



2.Check Session and disk freeable space


postgres=# select * from pg_stat_activity;
 datid | datname  |  pid  | usesysid | usename  | application_name | client_addr | client_hostname | client_port |         backend_start         |          xact_start           |          query_start          |         state_change          | waiting | state  |              query
-------+----------+-------+----------+----------+------------------+-------------+-----------------+-------------+-------------------------------+-------------------------------+-------------------------------+-------------------------------+---------+--------+----------------------------------
 12840 | postgres | 23155 |       10 | postgres | psql             |             |                 |          -1 | 2018-07-09 07:38:34.935179-04 | 2018-07-09 07:43:04.894374-04 | 2018-07-09 07:43:04.894374-04 | 2018-07-09 07:43:04.894378-04 | f       | active | select * from pg_stat_activity;
 12840 | postgres | 22809 |       10 | postgres | psql             |             |                 |          -1 | 2018-07-09 07:34:45.688671-04 |                               | 2018-07-09 07:37:37.758388-04 | 2018-07-09 07:37:37.758749-04 | f       | idle   | select oid,* from pg_tablespace;
(2 rows)




$ df -Th

Filesystem           Type   Size  Used Avail Use% Mounted on

/dev/xvde1           ext3   9.9G  6.4G  3.0G  69% /

none                 tmpfs   15G   12K   15G   1% /dev/shm

/dev/xvdl1           ext4   493G   47G  421G  10% /data/02

/dev/xvdk1           ext4   2.0T  1.8T  113G  94% /data/01




3.Create new tablespace location  /data/02 disk:

create tablespace  region owner denaliadmin location '/data/02/pgsql/data/base';

postgres=# \db+

                                  List of tablespaces

    Name    |    Owner    |         Location         | Access privileges | Description

------------+-------------+--------------------------+-------------------+-------------

 pg_default | postgres    |                          |                   |

 pg_global  | postgres    |                          |                   |

 region     | denaliadmin | /data/02/pgsql/data/base |                   |

(3 rows)




4.Move DB to new Tablespace


postgres=# select oid, * from pg_database;
  oid   |      datname       | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace |                              datacl
--------+--------------------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+-------------------------------------------------------------------
      1 | template1          |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | t            |           -1 |         12835 |    200001862 |          1 |          1663 | {=c/postgres,postgres=CTc/postgres}
  12835 | template0          |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | t             | f            |           -1 |         12835 |    200001940 |          1 |          1663 | {=c/postgres,postgres=CTc/postgres}
  12840 | postgres           |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    295302735 |          1 |          1663 |
  16384 | template_postgis   |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    205319808 |          1 |          1663 |
  21627 | denali_test        |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    205320018 |          1 |          1663 |
  17794 | denali             |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    205316770 |          1 |          1663 | {=Tc/postgres,postgres=CTc/postgres,r_denali_readonly=c/postgres}
  25419 | contrib_regression |     10 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    295302735 |          1 |          1663 |
  71746 | regression         |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    187750513 |          1 |          1663 |
 103050 | test               |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    200001862 |          1 |          1663 |
  48729 | region_na          |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    190246393 |          1 |          1663 |
 153385 | region_sea         |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    200001862 |          1 |          1663 |
 158397 | fuse               |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    200001862 |          1 |          1663 |
  81870 | region_eu          |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    192495454 |          1 |          1663 |
  93796 | region_sa          |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    200778866 |          1 |          1663 |
  ×××8 | region_mea         |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    190246488 |          1 |          1663 |
 101209 | region_il          |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    199337179 |          1 |          1663 |
 101862 | region_anz         |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    199763417 |          1 |          1663 |
(17 rows)


postgres=# select oid,* from pg_tablespace;

  oid   |  spcname   | spcowner | spcacl | spcoptions

--------+------------+----------+--------+------------

   1663 | pg_default |       10 |        |

   1664 | pg_global  |       10 |        |

 271240 | region     |    16513 |        |

(3 rows)



alter database region_il set tablespace region;

alter database region_anz set tablespace region;

alter database region_mea set tablespace region;

alter database region_sa set tablespace region;


postgres=# select oid, * from pg_database where datname in ('region_il','region_anz','region_mea','region_sa');
  oid   |  datname   | datdba | encoding | datcollate  |  datctype   | datistemplate | datallowconn | datconnlimit | datlastsysoid | datfrozenxid | datminmxid | dattablespace | datacl
--------+------------+--------+----------+-------------+-------------+---------------+--------------+--------------+---------------+--------------+------------+---------------+--------
 101209 | region_il  |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    199337179 |          1 |        271240 |
 101862 | region_anz |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    199763417 |          1 |        271240 |
  ×××8 | region_mea |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    190246488 |          1 |        271240 |
  93796 | region_sa  |  16513 |        6 | en_US.UTF-8 | en_US.UTF-8 | f             | t            |           -1 |         12835 |    200778866 |          1 |        271240 |
(4 rows)




postgres=# select oid,* from pg_tablespace;

  oid   |  spcname   | spcowner | spcacl | spcoptions

--------+------------+----------+--------+------------

   1663 | pg_default |       10 |        |

   1664 | pg_global  |       10 |        |

 271240 | region     |    16513 |        |

(3 rows)



$ df -Th

Filesystem           Type   Size  Used Avail Use% Mounted on

/dev/xvde1           ext3   9.9G  6.4G  3.0G  69% /

none                 tmpfs   15G   12K   15G   1% /dev/shm

/dev/xvdl1           ext4   493G  332G  136G  71% /data/02

/dev/xvdk1           ext4   2.0T  1.5T  399G  79% /data/01



5.Restart Database

pg_ctl stop;

pg_ctl start;


6.Reference

https://www.postgresql.org/docs/9.3/static/sql-createtablespace.html

https://www.postgresql.org/docs/9.3/static/sql-alterdatabase.html



相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
7月前
|
关系型数据库 MySQL Linux
TiDB实时同步数据到PostgreSQL(三) ---- 使用pgloader迁移数据
使用PostgreSQL数据迁移神器pgloader从TiDB迁移数据到PostgreSQL,同时说明如何在最新的Rocky Linux 9(CentOS 9 stream也适用)上通过源码编译安装pgloader。
251 0
|
10月前
|
数据可视化 关系型数据库 MySQL
将 PostgreSQL 迁移到 MySQL 数据库
将 PostgreSQL 迁移到 MySQL 数据库
1088 2
|
4月前
|
关系型数据库 数据库 PostgreSQL
PostgreSQL从小白到高手教程 - 第41讲:postgres表空间备份与恢复
PostgreSQL从小白到高手教程 - 第41讲:postgres表空间备份与恢复
139 1
|
5月前
|
SQL 关系型数据库 MySQL
MySQL【实践 02】MySQL迁移到PostgreSQL数据库的语法调整说明及脚本分享(通过bat命令修改mapper文件内的SQL语法)
MySQL【实践 02】MySQL迁移到PostgreSQL数据库的语法调整说明及脚本分享(通过bat命令修改mapper文件内的SQL语法)
127 0
|
5月前
|
关系型数据库 数据库连接 分布式数据库
我想迁移下数据,用新镜像启动下PolarDB,这个有什么好的办法吗?
我想迁移下数据,用新镜像启动下PolarDB,这个有什么好的办法吗?
38 0
|
10月前
|
存储 关系型数据库 Java
postgresql清理表空间
postgresql清理表空间
195 0
|
5月前
|
SQL 关系型数据库 Apache
Microsoft SQL Server 迁移到 PostgreSQL 利器:Babelfish
Babelfish for PostgreSQL 加速了将 Microsoft SQL Server 应用程序迁移到 PostgreSQL 的旅程。Babelfish for PostgreSQL 是一个开源项目,可在 Apache 2.0 和 PostgreSQL 许可下使用。它使 PostgreSQL 能够理解来自 Microsoft SQL Server 编写的应用程序的查询。Babelfish 了解 SQL Server 有线协议和 Microsoft SQL Server 查询语言 T-SQL,此您不必切换数据库驱动程序或重新编写所有应用程序查询。
332 0
|
8月前
|
Oracle 关系型数据库 分布式数据库
如何从Oracle迁移到PolarDB(ADAM)(二)
如何从Oracle迁移到PolarDB(ADAM)(二)
130 0
|
8月前
|
SQL Oracle 关系型数据库
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
Polar DB-O (兼容 Oracle 语法版本)和Polar DB PostgreSQL 版本概述(二)
747 0
|
9月前
|
存储 关系型数据库 数据库
PostgreSQL 中,表空间
PostgreSQL 中,表空间
88 1