Skip to content
Download all

Your server is running. In section 2 you saw its parts on a diagram. Now find each one on your own server: the processes, the memory and the files. First, meet psql, the tool you use to ask the server questions.

Definition: psql is PostgreSQL's command-line client. A role is a database user. The install creates one superuser role called postgres, matching the Linux user postgres.

psql over the local socket
pg_hba METHOD = peer

Linux user: postgres

Database role: postgres

Peer authentication asks Linux "who is this?" and lets you in as the role with the same name. That is why the postgres Linux user needs no password to connect locally.

On suta-linux-lab-1, as student:

suta-linux-lab-1 - as student
# Switch to the postgres user (type exit to come back)
sudo su - postgres
  • su - switches to another user with that user's full login settings.
  • Your prompt changes, and whoami now prints postgres.

Now as postgres on suta-linux-lab-1:

suta-linux-lab-1 - as postgres
# Open an interactive psql session as the postgres role
psql

Expected output:

psql (18.7)
Type "help" for help.
postgres=#

The prompt postgres=# means: connected to database postgres, and # means you are a superuser.

Leave psql, but stay the postgres user for the rest of this section. \q is a psql command (it starts with a backslash) that quits psql:

psql - as postgres
-- Quit psql
\q

Back to the theory: 2.1 Processes.

Still as postgres on suta-linux-lab-1:

suta-linux-lab-1 - as postgres
# Ask PostgreSQL which processes are running, by type
psql -c 'SELECT pid, backend_type FROM pg_stat_activity ORDER BY backend_type;'
  • psql -c runs one SQL command and exits.
  • pg_stat_activity is a built-in view with one row per server process.

Expected output (your pid numbers will differ):

pid | backend_type
------+------------------------------
939 | autovacuum launcher
936 | background writer
935 | checkpointer
1207 | client backend
933 | io worker
934 | io worker
932 | io worker
940 | logical replication launcher
938 | walwriter
(9 rows)

The client backend row is you: the psql you just ran. The postmaster and the logger are not in this view, so while your psql runs the server has 11 processes: these 9, plus the postmaster and the logger. When no client is connected, it has 10.

Back to the theory: 2.2 Memory.

Still as postgres on suta-linux-lab-1:

suta-linux-lab-1 - as postgres
# Show the size of the shared data cache
psql -c 'SHOW shared_buffers;'

Expected output:

shared_buffers
----------------
128MB
(1 row)

Back to the theory: 2.3 Storage.

On disk, each database is a folder under base, named by a number: its OID (object identifier), the number PostgreSQL gives every database. Still as postgres on suta-linux-lab-1:

suta-linux-lab-1 - as postgres
# Match each database to its folder number
psql -c 'SELECT oid, datname FROM pg_database ORDER BY oid;'

Expected output:

oid | datname
-----+-----------
1 | template1
4 | template0
5 | postgres
(3 rows)

$PGDATA is a shell variable: a name that holds a value, here the path of the data directory. A $ in front of a name means "use its value". The postgres login sets PGDATA for you. Check it:

suta-linux-lab-1 - as postgres
# Print the value of the PGDATA variable
echo $PGDATA

Expected output:

/var/lib/pgsql/18/data
suta-linux-lab-1 - as postgres
# One folder per database, named by its oid
ls $PGDATA/base

Expected output:

1 4 5

When you are done, return to student:

suta-linux-lab-1 - as postgres
# Return to the student user
exit

Was this page helpful?

Thanks. Glad it helped.

Thanks for telling us. Email the instructor what was missing, or ask in class.