PostgreSQL understanding of the relationship between the schemas, tables, space, user

In the usual work, we often come into contact 数据库表and 用户as well as 角色the use of default due to frequent use 数据库表空间and 模式(Schema), so we tend to ignore the concept and the role of the database table space and schema.

Next, tell us about the definition and role models and table space.

What is a Schema?

A database that contains one or more named model, which in turn contain tables. Mode may also contain other objects, including data 类型, 函数, 操作符and the like. The same object name can be used without causing conflicts in different modes; for example, herschemaand myschemacan contain named mytabletable. And databases, the model is not strict separation: as long as you have permission, a user can access the database objects of any pattern he connected in. There are many reasons we need to model:

  • It allows multiple users to use a database without interfering with other users.
  • The database objects are organized into logical groups to make them more manageable.
  • Third-party applications can be placed in different modes, so they do not conflict and names of other objects.

Mode is similar to an operating system-level directory, but the pattern can not be nested.

What is table space?

Table space is where the actual data is stored. A database schemamay exist in a plurality of table space, similarly, a space table may be a plurality of schemaservices.

By using tablespaces , an administrator can control the layout of the disk. The most common role is to optimize the performance table space, for example, one of the most commonly used index can be built on a very fast hard disk, and less frequently used tables can be built on the cheap hard drives, such as to store files for archiving table.

The relationship between PostgreSQL tablespace, database, schema, table, user, role

Relationship with the user's role

In the PostgreSQLmiddle, there are two confusing concepts: Role / user. The reason why these two concepts easily confused, because for PostgreSQL, this is exactly the same two objects. The only difference is when it is created:

1. I use the following psqlto create a role custom:

CREATE ROLE custom PASSWORD 'custom';

Then I use the newly created role of custom login, PostgreSQL refused to give information:

FATAL:role 'custom' is not permitted to log in.

Indicating that the role does not have permission to log on, the system rejects their login

2. I use the following psqlto create a user guest:

CREATE USER guest PASSWORD 'guest';

Then I use a guest login, the login is successful

Is there a difference both? View documents, and such a description: the CREATE the USER IS at The Same, the except that the ROLE AS IT the implies the CREATE LOGIN. ---- CREATE USERIn addition to having the default LOGINpermissions, the other with CREATE ROLEexactly the same.

In order to verify this statement, modify custompermissions, increase LOGINpermissions:

ALTER ROLE custom LOGIN;

Again with customlogin, success! So clear things up:

CREATE ROLE custom PASSWORD 'custom' LOGIN 等同于 CREATE USER custom PASSWORD 'custom'.

This is the ROLE/USERdifference.

Relational Database and Model

模式(schema)It is a logical division of the database (database).

While the database was created, it has created a database default mode - publicThis is the default mode of the database. All objects (tables, functions, trying, indexes, sequences, etc.) created for this database are created in this mode:

1. Create a database mars

CREATE DATABASE mars;

2. customRole log on to the marsdatabase to see all models database: \ dn

The results show that only publica model.

3. Create a test table

CREATE TABLE test(id integer not null);

4. Check the list of current database: \ d;

The results are part of the display table test mode public. That is testthe table is created by default in a public mode of.

5. Create a new model custom, corresponding to the logged-on user custom:

CREATE SCHEMA custom;

ALTER SCHEMA custom OWNER TO custom;

6. Create a re- testtable, this table to indicate the mode

CREATE TABLE custom.test (id integer not null);

7. Review the list of current database: \ d

The results are displayed in Table testbelongs mode custom. This is the testtable is created in the custom模式middle.

It concluded that: the database is being mode (schema) to segmentation, a database has at least one mode, all internal database objects (object) is created in the model. After the user logs into the system, connected to a database, the database through search_path to find schema search order can be ordered SHOW search_path; a particular order, can also SET search_path TO 'schema_name'be modified order.

Official advice is this: After the administrator to create a specific database, should be able to connect to all users of the database are the same as the user name to create a pattern, then, will be search_pathset $user, that is, the default mode is the same as the user name mode.

Table space and database

Database creation statement:

CREATE DATABASE dbname;

The default database owner is the current role of the database is created, the default table space is the default table space systems The pg_default tablespace is .

Why is this?

Because PostgreSQL, the data is created by cloning a database template to achieve, which is SQL SERVER is the same mechanism. Because CREATE DATABASE dbnameit does not specify a database template, so the system will default to clone template1a database, get a new database dbname. (By default, the new database will be created by cloning the standard system database template1)

template1The default database table space is pg_default, the table space is created when the database is initialized, all template1the objects will be synchronized cloned into the new database.

Relatively complete syntax should look like this:

CREATE DATABASE dbname TEMPLATE template1 TABLESPACE tablespacename;
ALTER DATABASE dbname OWNER TO custom;

1. Connect to the template1database, create a table as a marker:

CREATE TABLE test(id integer not null);

Inserting data into the table

INSERT INTO test VALUES (1);

2. Create a table space:

CREATE TABLESPACE tsmars OWNER custom LOCATION '/tmp/data/tsmars';

Prior to this should ensure that the directory / tmp / data / tsmars exist, and the directory is empty.

3. Create a database, specify the database table space is just created tsmars:

CREATE DATABASE dbmars TEMPLATE template1 OWNERE custom TABLESPACE tsmars;
ALTER DATABASE dbmars OWNER TO custom;

4. Review the information about all databases: \ l +

Can be found in dbmarsthe database table space is tsmars, the owner is custom;

After careful analysis, it is easy to conclude:

In PostgreSQL, a table space is a directory, which is stored in various databases it contains the physical file .

to sum up

Table space is a storage area in a table space can be stored in multiple databases, PostgreSQL although not recommended, but we do so entirely feasible. A database and object storage table structure directly know the like, but in at least one logical database model is created, the tables and other objects created in the model, the different patterns have been assigned different roles, privilege separation can be achieved, but also by authorizing, shared among schema objects, and there is a feature: public mode can store objects we all need to access.

Table space for the location of objects in the database define a physical storage device, is not specific to a single database. Physical database is a collection of database objects, and schemais a logical set of internal database for the organization and management of database objects, schema name space is under various application programs will come into contact with objects such as tables, indexes, data types, functions, operators and the like.

Role (user) is the database server (cluster) access control systems globally for all objects within a variety of cluster-wide rights management. So the role is not specific to a single database, but the role if you need to log database management system must be connected to a database. Roles can have a variety of database objects.

Welcome to my personal blog

No public concern: JAVA half past nine classrooms , there are a number of outstanding technical Daniel, provide direction for you, provide resources! Join us to explore technological progress together! Reply to "profile" the industry 2T get the latest information!

Guess you like

Origin www.cnblogs.com/noodlesmars/p/11850559.html