PostgreSQL Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 63: Line 63:


=Schema=
=Schema=
<font color=darkkhaki>What is a schema?</font>


Each database has by default a public schema.
Each database has by default a public schema.

Revision as of 22:38, 13 October 2023

External

Internal

Cluster

A collection of databases and global SQL objects, and their common static and dynamic metadata. A clusters is also referred to as instance. There is no relationship within the PostgreSQL cluster and the SQL term "CLUSTER".

Cluster Operations

Instance

An PostgreSQL instance is a group of backend and auxiliary processes that communicate using a common shared memory area. There is one postmaster process that manages the instance. One instance manages exactly one database cluster with all its databases. Many instances can run on the same server machine as long as the TCP ports do not conflict. The instance handles all key features of a RDBMS: read and write access to files and shared memory, enforcement of ACID properties, connections to clients, privilege verification, crash recovery, replication, etc.

Session

A database client must establish a session to the PostgreSQL instance before being able to perform data operations with SQL commands. The session implies a lower-level connection to the instance backend, over local or network socket. Session and connection are used interchangeably.

Connection

A connection is an established line of communication between a client process and a backend process, usually over a network, supporting a session. Session and connection are used interchangeably. There are two types of connections, over a local socket and over network using the TCP protocol.

Local Socket Connection

This is the default type of connection, when the psql client and the PostgreSQL instance run on the same machine.

TCP Network Connection

The default port is 5432.

Identity and Permissions

Authentication

User

Users are shared across databases.

The "user" concept is equivalent with the "role" concept. They mean the same thing.

User Operations
Master User

An administrative user that exists when the RDBMS instance is created and that has privileges to create other database and other users. It is used to bootstrap the administration of the RDBMS instance, by defining all users, objects, and permissions in the databases of your DB instance. Master Username must start with a letter. The RDS documentation refers to it as "Master username".

Role

The "role" concept is equivalent with the "user" concept. They mean the same thing.

Role Attributes

A specific role may:

  • be a superuser
  • create another role
  • create a database

Instance Operations

Database

https://www.postgresql.org/docs/14/manage-ag-overview.html

A database is a named collection of local SQL objects. A local SQL object is any object that can be created with an SQL CREATE command.

A PostgresSQL usually comes with 4 pre-existing databases (postgres, admin, template0, template1). "postgres" is fit for general use and it should be used by default.

Database Name Case Sensitivity

Verify this:

It seems that the database name is case sensitive, even if a database is created with "CREATE DATABASE TEST_DB", the database name becomes "test_db", and this is what it should be used in the connect URL. "jdbc:postgres://localhost/test_db" will work, but "jdbc:postgres://localhost/TEST_DB" won't.

Database Operations

Schema

What is a schema?

Each database has by default a public schema.

Tablespace

Data Types

https://www.postgresql.org/docs/16/datatype.html

For CREATE TABLE syntax see:

Create a PostgreSQL Table

Identity

Timestamp

Character Types

https://www.postgresql.org/docs/16/datatype-character.html

Variable Length with Limit

A type designated by character varying(n) or varchar(n). The forms are equivalent. This type can store strings up to n characters (not bytes) in length. An attempt to store a longer string into a column of these types will result in an error, unless the excess characters are all spaces, in which case the string will be truncated to the maximum length. If the string is shorter than n, character varying column will store the shorter string.

Fixed-Length with Blank Padding

A type designated by character(n), char(n) or bpchar(n). It stores fixed-length, blank-padded strings of length n, which represents characters, not bytes.

Variable Unlimited Length

The type is designated by text. This type stores strings of any length. It is not in the SQL standard. text is PostgreSQL's native string data type.

Numeric Types

SMALLINT

2 byte integer.

INT

4 byte integer. This is a typical choice for integers.

BIGINT

8 byte integer.

DECIMAL

NUMERIC

REAL

DOUBLE PRECISION

SERIAL

BIGSERIAL

TODO

Environment Variables

PGDATA

Set it after installation, see:

PostgreSQL Installation on Mac