Course Content
ACID Properties and Query Processing
0/2
PostgreSQL Replication
0/1
PostgreSQL Upgrade
0/1
PostgreSQL Tutorial for Absolute Beginners [Administration]
About Lesson

PostgreSQL is implemented using a simple “process per-user” client/server model. In this model, every client process is connected to exactly one server process.

PostgreSQL Connection string

You need to specify the following parameters to connect to PostgreSQL:

  • Host or host address
  • Port
  • Database name
  • User
  • Password (or other means of authentication, if any)

Example., psql -U <username> -d <database name>

Step by Step:

  • The client process requests the master server process called the POSTMASTER process for a connection to the database instance.
  • The postmaster process creates a new backend process called the PostgreSQL process after the authentication check is done and if everything is fine, the connection is created.
  • The postmaster process will then assign the newly created backend process to the client process.
  • Once a connection is established the client process can send a query to the backend (server). The query is transmitted using plain text, i.e., there is no parsing done in the frontend (client).
  • The server parses the query, creates an execution plan, executes the plan and returns the retrieved rows to the client by transmitting them over the established connection

Further reading here

Few more commands to check which user and which database you are connected to?

How to check the current database?

hyd_db=>
hyd_db=> SELECT current_database();
current_database
------------------
hyd_db
(1 row)

How to check the current user?

hyd_db=> SELECT current_user;
current_user
--------------
hyd_user
(1 row)

How to check the current version?

hyd_db=> SELECT version();
version
----------------------------------------------------------------------
PostgreSQL 10.11 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), 64-bit

(1 row)

Examples notation

Notation Description
# shell prompt for Linux root user
$ shell prompt for Linux general user
postgres=# psql prompt for PostgreSQL administrator
postgres=> psql prompt for PostgreSQL general user

Help commands

postgres=# \?

ds[S+] [PATTERN] list sequences
dt[S+] [PATTERN] list tables
du[S+] [PATTERN] list roles

postgres=# \h
Available help:
ABORT CREATE FOREIGN TABLE
ALTER AGGREGATE CREATE FUNCTION DROP SEQUENCE

postgres=# \h ALTER DATABASE

Command: ALTER DATABASE
Description: change a database
Syntax:
ALTER DATABASE name [ [ WITH ] option [ … ] ]

where option can be:

ALLOW_CONNECTIONS allowconn
CONNECTION LIMIT connlimit
IS_TEMPLATE istemplate

ALTER DATABASE name RENAME TO new_name

ALTER DATABASE name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }

ALTER DATABASE name SET TABLESPACE new_tablespace

ALTER DATABASE name SET configuration_parameter { TO | = } { value | DEFAULT }
ALTER DATABASE name SET configuration_parameter FROM CURRENT
ALTER DATABASE name RESET configuration_parameter
ALTER DATABASE name RESET ALL……..