Sub topic 6
Roles and Databases in a PostgreSQL Cluster
Assume you can use a terminal, understand basic tables, and have PostgreSQL installed. This chapter explains how PostgreSQL organizes access before you create your practice database.
The PostgreSQL cluster
A PostgreSQL cluster is one running PostgreSQL server and all the databases it manages. The word cluster does not necessarily mean several computers. On a local installation, one cluster usually runs on your computer and listens on port 5432.
A cluster contains:
- Databases, which hold schemas, tables, views, and other objects.
- Roles, which are identities used to log in and control permissions.
- Configuration, such as connection settings and authentication rules.
A role can be a person, an application, or an administrator. PostgreSQL uses the same role system for users and groups. A role with the LOGIN attribute can start a connection. A role without LOGIN can act as a permission group.
Roles and databases are different
A role is not a database. A role answers, “Who is connecting?” A database answers, “Which collection of objects should this session use?”
| Object | Main purpose | Example |
|---|---|---|
| Role | Identifies a client and controls access | `postgres` |
| Database | Stores schemas and tables | `firstdb` |
| Table | Stores rows inside a database | `books` |
A role can connect to more than one database if it has permission. A database can be accessed by more than one role. However, every connection uses one database and one current role.
The postgres role is commonly created during installation. The postgres database is also commonly available for administration. Names and permissions can differ by operating system and installation method, so do not assume that every local setup is identical.
Inspect the current connection
Start psql with a database and role that exist on your system:
psql -h localhost -p 5432 -U postgres -d postgresThe options mean:
-h localhostselects the server on your computer.-p 5432selects the server port.-U postgresselects the login role.-d postgresselects the database.
Inside psql, SQL statements end with semicolons. You can check the identity and database for the current session:
SELECT current_user, current_database();You can also list databases and roles with psql meta-commands. A meta-command begins with a backslash and is interpreted by psql, not by the PostgreSQL SQL engine.
\l
\du\l lists databases. \du lists roles and their attributes.
Connecting to another database
A session can switch databases with the \c meta-command, provided the selected role is allowed to connect:
\c firstdbAfter this command, queries run against firstdb, while the role remains the same unless you specify another role. Database objects are separate: a table created in firstdb does not automatically appear in postgres.
Exercise
Exercise
What is the difference between the postgres role and the postgres database?
Show answer
The role is an identity used to connect and receive permissions. The database is a collection of schemas and data that the session can use.