postgresql database table space migration

Background problem

Pg database in practical use, with the continuous accumulation of business data, will lead to the available disk space getting smaller and smaller, especially in linux, no way the original disk expansion, can only come by loading new disk for expansion. If the original pg database is a cluster, it can be handled well, one is part of a cluster database moved away, while another is described herein mentioned table space migration.

analysis

pg database is generally installed by default after two table spaces, pg_default and pg_golebal. Physical positions of the two table space by default in the data directory. Without additional configuration, then all data will exist in pg_default. pg database allows users to create their own table space, specify the physical location. The advantage is that you can make good database performance ssd frequently used to mount the disk, the other can be placed in the hdd, fully taking into account the needs of the business. Of course, data can be migrated, which is described below.

Scenes

Main work in linux system from pg database to store data in constant process, mounted under / data1 database pgdata growing space, disk monitoring alarm, in order to ensure uninterrupted service, data now needs to be migrate.

solve:

  • In the first linux system / data2 directory to mount a new disk. This step needs to be synchronized from the database.
mkfs.ext4 /dev/sdc
mount /data2 /dev/sdc
  • Create a new directory as your data directory table space in the / data2, and give postgres privileges. This step also needs to be synchronized from the database.
mkdir /data2/pg_tbs1
chown -R postgres.postgres /data2/pg_tbs1
chmod 700 /data2/pg_tbs1
  • Create a new table space, and specify the newly created directory, this can be carried out in the master database, from automatically synchronized.

postgres=# create tablespace tbs_data location '/data2/pg_tbs1';
CREATE TABLESPACE
  • View table space
postgres=# \db+
                                     List of tablespaces
    Name    |  Owner   |    Location    | Access privileges | Options |  Size   | Description 
------------+----------+----------------+-------------------+---------+---------+-------------
 pg_default | postgres |                |                   |         | 15 GB   | 
 pg_global  | postgres |                |                   |         | 521 kB  | 
 tbs_data   | postgres | /data2/pg_tbs1 |                   |         | 0 bytes | 
(3 rows)
  • Database migration, the process is quite fast, the basic is copied. Similarly, only you need to be on the line in the main library will automatically sync from the library. Note that the migration process, TEST library will add global lock can not read and write. So it is necessary to coordinate the traffic free time in practice.
迁移数据库TEST的表空间
alter database TEST set tablespace tbs_data;
  • View the migration is complete, nearly 12G database migration to the new disk, the disk space of the original release came out.
postgres=# \db+
                                     List of tablespaces
    Name    |  Owner   |    Location    | Access privileges | Options |  Size   | Description 
------------+----------+----------------+-------------------+---------+---------+-------------
 pg_default | postgres |                |                   |         | 3043 MB | 
 pg_global  | postgres |                |                   |         | 521 kB  | 
 tbs_data   | postgres | /data2/pg_tbs1 |                   |         | 12 GB   | 
(3 rows)

other

Overall, this method is very simple, than separates directly on database easier to achieve.

Guess you like

Origin www.cnblogs.com/easonbook/p/11660987.html