01 Lesson 01: Basic Tools / Server Installation and Management
This page is extracted from the official PDF into native Markdown. Slide text, code and speaker notes are kept in reading order; the original PDF and HTML remain the layout references. HTML source: dba1_01_tools_install.html Archive: DBA1-handouts-16-en.zip PDF pages: 23
Page 1: Basic Tools / Server Installation and Management
Copyright
© Postgres Professional, 2017–2025 Authors: Egor Rogov, Pavel Luzanov, Ilya Bashtanov, Aleksei Beresnev Translated by: Liudmila Mantrova, Alexander Meleshko, Elena Sharafutdinova Photo: Oleg Bartunov (Phu Monastery and Bhrikuti Peak, Nepal)
Use of Course Materials
Non-commercial use of course materials (presentations, demonstrations) is allowed without restrictions. Commercial use is possible only with the written permission of Postgres Professional. It is prohibited to make changes to the course materials.
Feedback
Please send your feedback, comments and suggestions to: edu@postgrespro.ru
Disclaimer
Postgres Professional assumes no responsibility for any damages and losses, including loss of income, caused by direct or indirect, intentional or accidental use of course materials. Postgres Professional company specifically disclaims any warranties on course materials. Course materials are provided “as is”, and Postgres Professional company has no obligations to provide maintenance, support, updates, enhancements, or modifications.
Page 2: Topics
- Basic Сoncepts
- Installation from Source Code
- Server Management
- Package Installation
- Server Management in Ubuntu
- Cloud Solutions
Page 3: DB Cluster and Server
{#source-figure-003 caption=“Source slide page 3: DB Cluster and Server”}
Speaker notes
Before talking about installation, let’s look at some basic concepts.
PostgreSQL is a program that belongs to the class of database management systems.
When this program is running, we call it a PostgreSQL server or a server instance. So far, the server seems to be a “black box” for us, but gradually we will get acquainted with how it works.
The data managed by PostgreSQL is stored in databases. One instance of PostgreSQL simultaneously manages several databases. This set of databases is called a database cluster. We will talk more about databases in the lesson Data Organization. Databases and Schemas.
So, a database cluster is data in files. A server or a server instance is a program that manages a database cluster.
Page 4: Source Code
Installation from source code
- stable server version
- you can use non-standard parameters
- or build the server for a non-standard architecture
- Installation from git
- current server version
- used primarily by kernel developers; requires a broader set of tools
Speaker notes
You should at least have an idea of how to install the program from source code, so that you could build PostgreSQL with non-standard parameters or on a non-standard architecture, if necessary.
You can find the source code in 2 formats: gzip and bzip2 at https://www.postgresql.org/ftp/source, as well as in the VM in the student user’s home directory.
You can also get the source code straight from the project’s git repository: git://git.postgresql.org/git/postgresql.git (there are other mirrors, including github).
This way you can build not just the stable version, but also a version at any specific commit, including the most recent one.
When installing from the git repository, a wider set of tools is required. For example, the lexer and parser are made using Flex and Bison, and git stores the source codes for these tools. They generate C files when built, which are then compiled. The source code archives include the pre-built C files already.
Page 5: Required Software
Hard requirements
- tar, gzip/bzip2, GNU make, C compiler (C89)
- Used but can be disabled
- GNU Readline, zlib, lz4, and zstd libraries
- Extras
- Perl, Python, and Tcl programming languages
- for using PL/Perl, PL/Python, PL/Tcl
- Kerberos, OpenSSL, OpenLDAP, PAM for authentication and encryption
- ICU library for cross-platform UNICODE support
- Separate tools when building from the git repository
Speaker notes
The configuration and installation process is traditionally based on GNU Make and Autoconf:
https://postgrespro.com/docs/postgresql/16/install-make
This course covers the traditional build method.
Starting with version 16, PostgreSQL also supports the Meson build system as an alternative.
https://postgrespro.com/docs/postgresql/16/install-meson
A number of programs and utilities are required when building from source.
The readline library allows you to edit the command line, use the command history and auto-completion. In a server installation, it may not be necessary if you are never going to run psql via the console.
The libraries zlib, lz4, and zstd are used for compression.
Page 6: Unpacking the Archive
The source code package is located in the student user’s home directory. Let’s unpack and open it:
Configuration
We’ll set up the configuration process in the directory containing the unpacked PostgreSQL source code using the configure script.
Important Note! If rebuilding (e.g., with different configuration parameters), you must first remove files from previous configuration to avoid conflicts:
The configure command accepts a number of parameters. Example: –prefix — installation directory, default/usr/local/pgsql –enable-debug — to enable debug information See the documentation for the full list of parameters. The command also makes use of environment variables. For example, CC and CFLAGS configure the C compiler. By default, the configure script prepares compilation and installation, placing all executable files, libraries, and other PostgreSQL files in the directory: /usr/local/pgsql. Typically, the installation directory is accessible for writing only to the OS superuser. For demonstration purposes, we use the –prefix option to specify the server software installation directory in the pgsql16 subdirectory of the student user’s home directory. We are also using port 5555 instead of the default 5432:
The output of this command is extremely verbose, so we are skipping it entirely.
PostgreSQL Compilation and Installation
Options: make — build only the server make world — build the server, all extensions and documentation Let’s compile the server. Depending on your computer performance, this may take minutes or even tens of minutes.
Now, install the server.
To compile the extensions, repeat the make and make install commands from within the contrib directory.
Page 7: Page 7
If you build the server together with the extensions and docs (make world), you can install everything with just the make install-world command.
Page 8: Creating a Сluster
{#source-figure-008 caption=“Source slide page 8: Creating a Сluster”}
Tool
- initdb
- Features
- a new database cluster cannot belong to the OS superuser
- PGDATA is a convenient variable that represents the cluster directory
- configuration files are created in the PGDATA directory
- enabling checksum calculation for data pages is a good idea
Speaker notes
After installing the server, you need to create a database cluster. The initdb tool does that for you.
For security reasons, the directory in which the cluster is initialized cannot belong to the OS superuser. Usually, the cluster ownership is assigned to the postgres user.
The cluster owner can define the PGDATA environment variable pointing to the cluster directory. This variable is used by some server tools when they need to find out the location of the cluster. Such tools include initdb, as well as pg_ctl, the main server management tool, which we will discuss in a bit.
During cluster initialization, initdb creates configuration files in the PGDATA directory. We will talk more about configuration files in another lesson.
Initdb has many keys that affect its operation. One important key is -k or –data-checksums. It enables or disables calculating checksums for data pages. The checksum check is performed when accessing any data page in the cluster. This slightly reduces performance, but allows you to quickly detect data corruption.
For more details, see https://postgrespro.com/docs/postgresql/16/app-initdb
Page 9: Creating a Cluster
Now, let’s create a data directory. In production, it’s a good practice to have a separate OS user as the owner of the data directory. Here, we will just use the student user and the /home/student/pgsql16 directory.
This directory is often referred to as PGDATA. PGDATA is the environment variable pointing to it, and it’s a convenient shortcut.
All server tools and utilities are located in the bin directory. We recommend you to add it into the PATH variable:
initdb is a tool used to initialize a database cluster. In the -U option, you can specify the name of the DBMS superuser. If you do not specify it, then the OS username is used, in our case student. Usually this user is postgres, so let’s set this name explicitly. The -k option includes page checksum calculation, which allows timely detection of data corruption. If PGDATA is not set, specify a directory for the data in the -D option. In our case, we do not actually need to specify this option.
Page 10: Server Management
Tool
- pg_ctl
- Main operations
- starting, stopping and checking the status of the server
- reloading configuration parameters
- switching to a standby
Speaker notes
The main server management operations include starting and stopping the server, getting the current status of the server, reloading the configuration, and some others.
pg_ctl, a stock PostgreSQL tool, is designed to perform these actions.
pg_ctl should be run as the owner of the database cluster.
For more details about server management for database administrators, see:
https://postgrespro.com/docs/postgresql/16/app-pg-ctl
https://postgrespro.com/docs/postgresql/16/runtime
Page 11: Server Management
We can start the server now. In the -l option, specify the server message log file. The -D option is omitted because the PGDATA variable is set.
Let’s verify that the server is running: use the psql tool to connect to the server and return current time:
The following command stops the server:
Page 12: Package Installation
{#source-figure-012 caption=“Source slide page 12: Package Installation”}
Package installation is usually preferable
- Linux ( , Debian, Red Hat, SUSE and others)
- included in the OS distribution
- repository (yum, apt) or RPM/DEB packages
- FreeBSD, OpenBSD
- packages from the Ports and Packages Collection
- Mac OS X
- Windows
Speaker notes
The preferred option is to use ready-made packages, since in this case a clear, supported and easily updated installation is obtained.
Packages exist for most popular systems. Each of them may have its own features that you should get to know before installing.
Package repositories of some systems already include PostgreSQL, but usually not the latest version. The latest version is always available in the PostgreSQL Global Development Group (PGDG) repository:
https://www.postgresql.org/download/
We will be working with the Ubuntu PostgreSQL package.
Page 13: Creating a Cluster
{#source-figure-013 caption=“Source slide page 13: Creating a Cluster”}
Tool
- pg_createcluster
- initdb
- Features
- initialization is performed when installing the package, creates
- a cluster named “main”
- checksum calculation is not enabled by default
- defines the location of server configuration and log files
- sets up automatic server startup at OS startup
Speaker notes
pg_createcluster is a wrapper for the initdb tool designed to initialize a cluster in Ubuntu.
The help for the pg_createcluster command can be obtained using man: $ man pg_createcluster
pg_createcluster is executed automatically when the package is installed and creates a database cluster named “main”.
Note that cluster initialization is done with checksum calculation for data pages disabled.
Executables, configuration files and the server log are placed in accordance with the Ubuntu practices.
In addition, the installation sets up the PostgreSQL server to start and stop when Ubuntu starts and stops.
To delete a cluster, the pg_dropcluster tool is used.
Both pg_createcluster and pg_dropcluster are specific to Ubuntu. In other systems, you need to explicitly initialize a cluster using initdb and use the appropriate operating system tools.
Page 14: Creating a Cluster in Ubuntu
In the provided VM, PostgreSQL is installed from the distribution package:
PostgreSQL directory:
The owner of the server software is the root user. pg_config shows the parameters the server was compiled with:
A database cluster called main is already initialized and is located in /var/lib/postgresql/16/main. The owner of the directory is postgres user. Here’s what the directory contains:
Page 15: Page 15
Page 16: Server Management
Tool
- pg_ctlcluster
- pg_ctl
- Main operations
- starting, stopping and checking the status of the server
- reloading configuration parameters
- switching to a standby
Speaker notes
In the Ubuntu PostgreSQL package, pg_ctl is not run directly, but through a special wrapper pg_ctlcluster. The help for pg_ctlcluster can be obtained using man: $ man pg_ctlcluster
In other systems, pg_ctl can be used directly.
Page 17: Server Management in Ubuntu
With the package installation, PostgreSQL is automatically added to the OS startup list. So, there is no need to start it manually. You can explicitly control the server using the pg_ctlcluster tool, which must be run either as the OS postgres user or via sudo. The pg_ctlcluster tool can manage multiple servers of different versions. It has the following parameters: 16 — server version number main — server name Command Stop:
Start:
Restart:
Get server status:
Reload configuration:
For a package installation, the server log file is located at:
Let’s look at the last messages:
Page 18: Cloud Solutions
PostgreSQL in a virtual environment
- virtualization overhead
- the administrator has full access to the instance
- PostgreSQL as a Service
- offered by many cloud providers
- the provider takes over the administration routine
- limited management, backup and monitoring tools
Speaker notes
Virtualization solutions can be used to run databases, including PostgreSQL. The trade off for the convenience of this approach is considerable overhead costs. For high-load systems, any additional intermediate layers between the DBMS and the hardware are undesirable.
In addition, many leading cloud providers offer PostgreSQL as a service (Database as a Service, managed database).
In this case, the provider takes over most of the administration tasks. This limits your control over the instance. Moreover, management capabilities for tasks such as performance monitoring or backup and recovery are limited by the tools offered by the service provider.
We will not consider the features of individual cloud solutions within the course. You can study the documentation offered by different service providers to learn more.
Page 19: Takeaways
Two installation options — package or source code
- Cloud solutions available
- The server is run by a dedicated OS user
- Initialize a database cluster before using the server
- Specific commands for server management
Page 20: Practice
Enabling checksum calculation for a cluster.
- Stop the server.
- Check whether checksums are calculated for the cluster.
- Enable checksum calculation.
- Start the server.
Speaker notes
2, 3. Use the pg_checksums tool. To run it, enter the full path: /usr/lib/postgresql/16/bin/pg_checksums
https://postgrespro.com/docs/postgresql/16/app-pgchecksums
Page 21: 1. Stopping the Server
2. Verification
To see if page checksum calculation is enabled, run the pg_checksums utility with the –check option:
3. Enabling Checksum Calculation
Run pg_checksums with the –enable option:
Checksum calculation is now enabled.
4. Starting the Server
Page 22: Practice+
- Install PostgreSQL from source code as shown in the demo.
- Create a database cluster, start the server.
- Make sure the server is running.
- Stop the server.
Page 23: 1. Compiling PostgreSQL from Source Code
All necessary commands were shown during the demo, but were not executed. Repeat the process by yourself within the provided virtual machine.
Note Note that compiling of the source code may take a while (several to tens of minutes, depending on your computer resources).