手把手教你:PostgreSQL在线扩容

转载自品略图书馆 http://www.pinlue.com/article/2020/06/1711/0010756417147.html

 

1. 查看表空间

 

 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
postgres=# \db+                                  List of tablespaces    Name    |  Owner   | Location | Access privileges | Options |  Size   | Description ------------+----------+----------+-------------------+---------+---------+------------- pg_default | postgres |          |                   |         | 1088 MB |  pg_global  | postgres |          |                   |         | 2167 kB | (2 rows)

 

可以看到这里没有自定义表空间,默认使用pg_default 表空间

2. 创建演示数据库和用户

 

使用新的数据库app,并且owner为app用户来进行演示

 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
postgres=# create user app password"XXX";CREATE ROLEpostgres=# create database app owner app;CREATE DATABASEpostgres=# \c app appYou are now connected to database "app" as user "app".app=> create schema app authorization app;CREATE SCHEMAapp=> create table public.t1(id int);CREATE TABLEapp=> create table app.t2(id int);CREATE TABLE

 

3. 新建表空间

 

创建表空间需要超级用户权限

 
  •  
  •  
  •  
  •  
$ mkdir tblspace_newapp=# CREATE TABLESPACE tbl_app OWNER app LOCATION "/home/postgres/tblspace_new";CREATE TABLESPACE

 

4. 数据库app使用新表空间作为默认表空间

 

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
app=# \c app appYou are now connected to database "app" as user "app".app=> alter database app set default_tablespace to tbl_app;退出psql再重新连接app=> create table public.t3(id int);CREATE TABLEapp=> create table app.t4(id int);CREATE TABLEapp=> select pg_relation_filepath("t3");              pg_relation_filepath              ------------------------------------------------ pg_tblspc/356916/PG_12_201909212/356883/356935(1 row)app=> select pg_relation_filepath("app.t4");              pg_relation_filepath              ------------------------------------------------ pg_tblspc/356916/PG_12_201909212/356883/356938(1 row)app=> select oid,spcname from pg_tablespace ;  oid   |  spcname   --------+------------   1663 | pg_default   1664 | pg_global 356916 | tbl_app(3 rows)
 

 

 

可以看到在app数据库下新建的表默认变到tbl_app表空间下,即oid为356916。

 

5. 修改用户app默认表空间为tbl_app

 

先恢复下上面的配置

 
  •  
  •  
  •  
  •  
  •  
  •  
drop table public.t3;drop table app.t4;alter database app set default_tablespace to default;app=# \c app appapp=> alter user app set default_tablespace to tbl_app;

 

退出psql再重新连接

 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
app=> create table public.t5(id int);CREATE TABLEapp=> create table app.t6(id int);CREATE TABLEapp=> select pg_relation_filepath("t5");              pg_relation_filepath              ------------------------------------------------ pg_tblspc/356916/PG_12_201909212/356883/356959(1 row)app=> select pg_relation_filepath("app.t6");              pg_relation_filepath              ------------------------------------------------ pg_tblspc/356916/PG_12_201909212/356883/356962(1 row)

 

可以看到用户app新建的表默认变到tbl_app表空间下,其他用户不受影响。

 

6. 总结

 

  • 如果要迁移数据表的表空间,会锁表,要注意对业务的影响。
  • 新建表修改database级别或者user级别(一般还是database级别)的默认表空间,只需配置一次,原有的数据表存储不改变,这种方式是推荐的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值