postgres Command in Linux



The postgres command in Linux starts the PostgreSQL database server. It handles client connections, manages database clusters, and can be configured using various command-line options or configuration files. It runs in the foreground by default but is usually set to run as a background process.

Table of Contents

Here is a comprehensive guide to the options available with the postgres command −

Note − The postgres command is a core part of PostgreSQL and is used to start the database server directly. However, in most production environments, it is recommended to use a system service manager systemctl or service to start and manage the PostgreSQL server instead of invoking postgres manually.

Initial Configuration

If the postgres − command not found error appears even after installing PostgreSQL, it needs to be manually set up. First, find the postgres binary file using the following command −

find /usr -name postgres 2>/dev/null
postgres Command in Linux1

Add the path to the bashrc file −

echo 'export PATH=$PATH:/usr/lib/postgresql/16/bin' >> ~/.bashrc
postgres Command in Linux2

Finally, reload the shell configuration by running −

source ~/.bashrc

Check whether the binary path of postgres appears, using the which command −

which postgres
postgres Command in Linux3

Syntax of postgres Command

The syntax of the postgres command in Linux is as follows −

postgres [options]

The postgres command in PostgreSQL comes with several options to configure and control how the database server behaves. These options set parameters for the server, such as memory allocation, connection settings, logging, and more. The [options] field in the above syntax is used to specify them.

postgres Command Options

The options for the postgres command in Linux are listed below −

Flags Options Description
-D datadir Specifies the file system location of the data directory.
-p port Defines the port number for client connections (default 5432).
-h hostname Specifies the IP address or hostname to listen on.
-c name=value Sets a runtime parameter for the PostgreSQL server.
-N max-connections Sets the maximum number of simultaneous client connections.
-s Enables printing time and statistics after each command.
-B nbuffers Defines the number of shared memory buffers used by PostgreSQL.
-F Disables fsync (improves performance but risks data corruption).
-e Sets the default date style to European (DD/MM/YYYY).
--single Starts PostgreSQL in single-user mode for maintenance or recovery.
-r filename Sends all server log output to the specified file in single-user mode.
-A 0|1 Enables or disables run-time assertion checks (debugging).
-d debug-level Sets the debug level for server logs (1 to 5).
-v protocol Specifies the frontend/backend protocol version.
-n Prevents the reinitialization of shared data structures (used for debugging).
-O Allows modification of system table structures (used during initialization).
-P Ignores system indexes when reading system tables.
-V --version Displays version.
-? --help Displays help and exits.

Examples of postgres Command in Linux

This section explores how to use the postgres command in Linux with examples −

  • Starting postgres in the Background
  • Starting postgres with a Specific Data Directory
  • Starting postgres using a Specific Port
  • Setting postgres Run-time Parameters
  • Using Underscore or Dash for Parameters
  • Starting postgres in Single User Mode
  • Displaying Usage Help

Starting postgres in the Background

To start PostgreSQL in the background (daemon mode) with default settings, redirect output to a log file and avoid using the terminal −

nohup postgres > logfile 2>&1 </dev/null &
postgres Command in Linux4

To verify the operation, execute the following command −

ps aux | grep postgres
postgres Command in Linux5

Starting postgres with a Specific Data Directory

To start postgres with a specific data directory, use the -D option −

postgres -D /var/lib/postgresql/<version>/main

Replace the <version> in the above command with the installed version number of PostgreSQL.

To identify the data directory, first switch to the postgres user −

sudo -i -u postgres

Launch the PostgreSQL interactive terminal −

psql

In the interactive terminal, run the following command −

SHOW data_directory;
postgres Command in Linux6

Starting postgres using a Specific Port

To specify a custom port when starting PostgreSQL, use the -p option −

postgres -p 2341

Setting postgres Run-time Parameters

Run-time parameters can be set on the command line. For example, to change the work_mem parameter to 1024 kB −

postgres -c work_mem=1024

To list all the run-time parameters, switch to the postgres system user and then access psql

sudo -i -u postgres
psql

Now, run the following command −

SHOW ALL;
postgres Command in Linux7

Using Underscore or Dash for Parameters

Note that in PostgreSQL's command-line options, underscores and dashes can be used interchangeably in named parameters. For example, these two are equivalent −

postgres --work-mem=1024
postgres --work_mem=1024

Starting postgres in Single User Mode

In single-user mode, direct interaction with the database is possible. It is useful for debugging or recovery. To start the postgres in single-user mode, use the --single option −

postgres --single mydatabase

Similarly, to specify the data directory for single-user mode, use the -D option with the data directory path.

Displaying Usage Help

To display the usage help of the postgres command, use the -? or --help option −

postgres --help

Conclusion

The postgres command in Linux is used to start and manage the PostgreSQL database server. It allows configuring various settings through command-line options, such as specifying data directories, setting connection parameters, and enabling debugging.

The postgres command can run in the foreground or as a background process. While it provides direct control over the database server, using a service manager like systemctl is recommended in production environments.

Advertisements