Vous êtes sur la page 1sur 4

https://www.postgresql.org/docs/9.1/static/ddl-schemas.

html

cluster can have 1 to n db.

users and groups are shared across cluster.

But user can connect to db only he has access to.

why schema?

Many user to use same db without name collision.

CREATE SCHEMA myschema;

DROP SCHEMA myschema CASCADE;

if you want to create schema owned by another user other than you.

CREATE SCHEMA schemaname AUTHORIZATION username;

Public schema is default place where tables are created if we didnt specify schema.

CREATE TABLE myschema.mytable (

...

);

Search Paths

The system determines which table is meant by following a search path, which is a list of schemas to
look in,if the schema.mytable is referred as mytable.
The first schema named in the search path is called the current schema.

Aside from being the first schema searched, it is also the schema in which new tables will be created if
the CREATE TABLE command does not specify a schema name.

SHOW search_path;

o/p

search_path

--------------

"$user",public

The first element specifies that a schema with the same name as the current user is to be searched.

If no such schema exists, the entry is ignored. The second element refers to the public schema that we
have seen already.

SET search_path TO myschema,public;

Users and schema

----------------

N users can share a schema. But need permission to access the schema.

To allow that, the owner of the schema must grant the USAGE privilege on the schema.

To actually access the table ( any other db objects ) he needs certain specific grants.

A user can also be allowed to create objects in someone else's schema.


To allow that, the CREATE privilege on the schema needs to be granted.

NOTE: that by default, everyone has CREATE and USAGE privileges on the schema public.

PG_CATLOG -- is another default schema.

which contains the system tables and all the built-in data types, functions, and operators. pg_catalog is
always effectively part of the search path.

to simulate oracle env:

REVOKE CREATE ON SCHEMA public FROM PUBLIC;

DB and DB objects

-----------------

SELECT datname FROM pg_database;

Generally, every database object (tables, functions, etc.) belongs to one and only one database.

(However there are a few system catalogs, for example pg_database, that belong to a whole cluster and
are accessible from each database within the cluster.)

First db(postgres): created by initdb command.

template1 is template for other new db.

Tablespaces

-----------

CREATE TABLESPACE fastspace LOCATION '/mnt/sda1/postgresql/data';

CREATE TABLE foo(i int) TABLESPACE space1;


There is also a temp_tablespaces parameter, which determines the placement of temporary tables and
indexes, as well as temporary files that are used for purposes such as sorting large data sets.

This can be a list of tablespace names, rather than only one, so that the load associated with temporary
objects can be spread over multiple tablespaces.

A random member of the list is picked each time a temporary object is to be created.

Oracle system tb = pg_default

Two tablespaces are automatically created when the database cluster is initialized.

The pg_global tablespace is used for shared system catalogs.

The pg_default tablespace is the default tablespace of the template1 and template0 databases

Once created, a tablespace can be used from any database, provided the requesting user has sufficient
privilege.

This means that a tablespace cannot be dropped until all objects in all databases using the tablespace
have been removed.

SELECT spcname FROM pg_tablespace;

BKP

===

1) bkp ( Live: pg_dump dbname > outfile and psql dbname < infile , offline: )

2) statistics (vaccuming)

3) log maintenance (here is a built-in log rotation facility, which you can use by setting the configuration
parameter logging_collector to true in postgresql.conf)

4) reindex

Vous aimerez peut-être aussi