zoukankan      html  css  js  c++  java
  • postgresql 数据库,模式,表空间的关系

    数据库与模式
    模式(schema)是对数据库(database)逻辑分割
    在数据库创建的同时,就已经默认为数据库创建了一个模式--public,这也是该数据库的默认模式。所有为此数据库创建的对象(表、函数、试图、索引、序列等)都是常见在这个模式中的。
    test2_user=# create database mypg; #创建一个数据库
    CREATE DATABASE
    test2_user=# c mypg postgres #连接数据库
    You are now connected to database "mypg" as user "postgres".
    mypg=# dn
    List of schemas
    Name | Owner
    --------+----------
    public | postgres
    mypg=# CREATE TABLE test_a(id integer not null); #创建表,没有指定模式名,默认在public下
    CREATE TABLE
    mypg=# d
    List of relations
    Schema | Name | Type | Owner
    --------+--------+-------+----------
    public | test_a | table | postgres
    mypg=# create schema mypg; #创建模式mypg
    CREATE SCHEMA
    mypg=# create table mypg.b(id integer not null); #创建表的时候指定模式名
    CREATE TABLE
    mypg=# create table a(id integer not null);
    CREATE TABLE
    mypg=# d
    List of relations
    Schema | Name | Type | Owner
    --------+--------+-------+----------
    public | a | table | postgres
    public | test_a | table | postgres
    mypg=# set search_path to public,mypg; #指定搜索路径
    SET
    mypg=# d #查看数据库mypg的所有表
    List of relations
    Schema | Name | Type | Owner
    --------+--------+-------+----------
    mypg | b | table | postgres
    public | a | table | postgres
    public | test_a | table | postgres
    数据库是被模式(schema)来切分的,一个数据库至少有一个模式,所有数据库内部的对象(object)是被创建于模式的
    官方建议是这样的:在管理员创建一个具体数据库后,应该为所有可以连接到该数据库的用户分别创建一个与用户名相同的模式,然后,将search_path设置为"$user",
    =====
    表空间与数据库
    数据库创建语句CREATE DATABASE dbname 默认的数据库所有者是当前创建数据库的角色,默认的表空间是系统的默认表空间--pg_default
    mypg=# l
    List of databases
    Name | Owner | Encoding | Collate | Ctype | Access privileges
    ------------+------------+----------+-------------+-------------+---------------------------
    exampledb | dbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/dbuser +
    | | | | | dbuser=CTc/dbuser
    hq | dbuser | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
    mydb | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
    mypg | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
    postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
    template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
    | | | | | postgres=CTc/postgres
    template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
    | | | | | postgres=CTc/postgres
    test1_user | test1_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/test1_user +
    | | | | | test1_user=CTc/test1_user
    test2_user | test1_user | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
    (9 rows)
    在PostgreSQL中,数据的创建是通过克隆数据库模板来实现
    由于CREATE DATABASE dbname并没有指明数据库模板,所以系统将默认克隆template1数据库,得到新的数据库dbname
    而template1数据库的默认表空间是pg_default,这个表空间是在数据库初始化时创建的,所以所有template1中的对象将被同步克隆到新的数据库中
    mypg=# c template1 #连接数据库template1 
    template1=# d #下面没有任何表
    No relations found.
    template1=# dn
    List of schemas
    Name | Owner
    --------+----------
    public | postgres
    (1 row)

    template1=# CREATE TABLE yhq(id integer not null,date TIMESTAMP NOT NULL DEFAULT LOCALTIMESTAMP(0)); #在template1下面创建一个表
    CREATE TABLE
    template1=# INSERT INTO yhq (id) VALUES (1); #并插入一条记录
    INSERT 0 1
    template1=# select * from yhq;
    id | date
    ----+---------------------
    1 | 2018-12-13 16:03:10
    template1=# select *from pg_tablespace; #查看数据库的表空间
    spcname | spcowner | spcacl | spcoptions
    ------------+----------+--------+------------
    pg_default | 10 | |
    pg_global | 10 | |
    template1=# h create tablespace  #查看创建表空间的语法
    Command: CREATE TABLESPACE
    Description: define a new tablespace
    Syntax:
    CREATE TABLESPACE tablespace_name
    [ OWNER { new_owner | CURRENT_USER | SESSION_USER } ]
    LOCATION 'directory'
    [ WITH ( tablespace_option = value [, ... ] ) ]

    [root@mysqlhq local]# mkdir /usr/local/pgdata  #创建一个空目录
    [root@mysqlhq local]# chown postgres:postgres /usr/local/pgdata/ #指定权限

    template1=# create tablespace yhq owner postgres location '/usr/local/pgdata'; #创建一个表空间,并指定路径
    CREATE TABLESPACE
    template1=# CREATE DATABASE yhq1 TEMPLATE template1 OWNER postgres TABLESPACE yhq; #创建数据库,指定模板和表空间
    CREATE DATABASE
    template1=# l
    yhq1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
    template1=# c yhq1 #连接新创建的数据库
    You are now connected to database "yhq1" as user "postgres".
    yhq1=# d
    List of relations
    Schema | Name | Type | Owner
    --------+------+-------+----------
    public | yhq | table | postgres
    (1 row)

    yhq1=# select * from yhq; #里面确实存在数据,并确认来模板template1
    id | date
    ----+---------------------
    1 | 2018-12-13 16:03:10
    (1 row)
    [root@mysqlhq local]# cd pgdata/ #进入刚创建的表空间路径
    [root@mysqlhq pgdata]# ll
    total 0
    drwx------ 3 postgres postgres 19 Dec 13 16:11 PG_9.5_201510051
    [root@mysqlhq pgdata]# cd PG_9.5_201510051/
    [root@mysqlhq PG_9.5_201510051]# ll
    total 12
    drwx------ 2 postgres postgres 8192 Dec 13 16:11 25403  #这个路径下面,有一堆的文件

  • 相关阅读:
    防删没什么意思啊,直接写废你~
    绝大多数情况下,没有解决不了的问题,只有因为平时缺少练习而惧怕问题的复杂度,畏惧的心理让我们选择避让,采取并不那么好的方案去解决问题
    Java 模拟面试题
    Crossthread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on
    一步步从数据库备份恢复SharePoint Portal Server 2003
    【转】理解 JavaScript 闭包
    Just For Fun
    The database schema is too old to perform this operation in this SharePoint cluster. Please upgrade the database and...
    Hello World!
    使用filter筛选刚体碰撞
  • 原文地址:https://www.cnblogs.com/yhq1314/p/10114721.html
Copyright © 2011-2022 走看看