PostgreSQL relies on the operating system with file systems and underlying volumes servicing the persistent storage of row and columnar data.
PostgreSQL refers to the installation or instance as a cluster. This guide assumes that an operating system will have at most one cluster at any one time.
A PostgreSQL cluster consists of the following files :
- Configuration Files
- Write-Ahead-Log (Online and Archived) Files
- Tablespaces
- Server Log Files
There are a number of ways in which the file system layout for a PostgreSQL cluster can be established. The primary recommendation is that all volumes excluding the Write-Ahead-Log archive location should be located on the same FlashArray. The following table highlights the different approaches to the file system layouts for a PostgreSQL cluster.
|
Default layout All tablespaces , logs files , configuration files and the write-ahead-log will be placed in the same file system using the default directory structure for the operating system.
Note:
The recommendation for volumes from FlashArray is to mount a volume to the following locations with the correct permissions set before installing PostgreSQL : Microsoft Windows - C:\Program Files\PostgreSQL\ Permissions - PostgreSQL service account - READ Permissions on all directories leading up to the service directory, WRITE permissions are required only on the data directory) Linux - /var/lib/pgsql(Permissions - user postgres, group postgres, drwx------) |
|||||||||||||||||||||
|
Initializing PostgreSQL cluster at a different location This assumes that there is a new PostgreSQL deployment with no data or significant configuration that needs to be located in a volume or file system which is not in the default location. Note:
The steps will need to be repeated when performing package upgrades as the default PostgreSQL settings will overwrite these changes. Microsoft Windows In its default configuration with the PostgreSQL installation the data directory is created in C:\Program Files\PostgreSQL\<version>\ and owned by the Network Service. Initializing the cluster at a different storage location requires that a user with interactive logon permissions is present to perform command line and interactive operations. This user will also be given permissions on the intended destination location in order to operate the data directory.
When installing PostgreSQL from a installation manifest the minimum required users and groups will be automatically created. The permissions for the C:\PGSQL directory need to be given to the user under which the postgresql service runs as. In a default environment this is the NETWORK SERVICE user , which needs to be changed to the user who will run the initdb application(any user with interactive logon capabilities). A directory for the data directory also needs to be created in the new location and given the appropriate permissions. In this example the DBTest user is given the permissions to the C:\PGSQL\ and C:\PGSQL\Data directories and is the user under which the PostgreSQL service will run.
To initialize a new PostgreSQL cluster the initdb application is used. The initdb application can be found in the installation directory for PostgreSQL (default location: C:\Program Files\PostgreSQL\<Version>) in the bin directory. It can then be executed with the -D argument to initialize the data directory at a new location (note do not use the root of a filesystem). Open PowerShell and execute the following , adjusting as required with additional arguments :
If everything is successful the output should be similar to the following :
The service used to automatically start the PostgreSQL cluster needs to be deleted and recreated with the correct command to target the new PostgreSQL data directory location. To avoid conflicts with the old data directory the existing service needs to be deleted : In the Services management dialog the PostgreSQL cluster in this example is managed by the postgresql-x64-13 service :
This service needs to be deleted , which can be done in a command terminal like PowerShell (As an Administrator enter the cmd process) with the command :
If successful the prompt should respond with the following message :
To recreate the service with the new settings use the following command in the same command terminal, adjusting the user (-U) and data directory (-D) as required :
This recreates the windows service with the correct data directory and user startup permissions.
Before starting the service for the first time the user must have the password entered for it , so that the service can start correctly. This can be done by going into the service properties and navigating to the Log On section. In the Log On section provide the password for the user account and select OK.
At this point the service can be started and will now use the new data directory location. Linux The postgres user which is a member of the postgres group is created with the home directory : /var/lib/pgsql/ (also the default location for the data directory under the /version/ folder) during installation from a package or package manager. This user will be used to provide the correct permissions for the intended destination location.
This user and group need to be given ownership of /pgsql and its contents (a directory called data needs to be created before running the initdb application) once a FlashArray volume has been mounted to it. This is done using the chown command :
The PGDATA environment variable needs to be changed so that it persists across sessions and is used correctly when starting the PostgreSQL cluster. For systemd based systems the service configuration file needs to be altered to ensure the new database directory is used. Persistently set PGDATA environment variables Note:
Note the PGDATA path in linux environments must not contain spaces. The PGDATA environment variable is set in the following locations and needs to be changed : ~/.bash_profile
(SystemD) Change database directory (As root) Identify the service file under which PostgreSQL is started in systemd (this is the .service file found after the Loaded section) :
Edit the service file , altering the Environment=PGDATA= line :
Save the file and reload SystemD :
(SELinux enabled systems only) - Change SELinux configuration to avoid service startup issues Get the SElinux labeling for the default data directory with the following command:
Once you have the label change it with the following command:
The initdb application is apart of the postgresql-<version>-setup binary. This can be located in /usr/pgsql-<version>/bin. The data directory can only be initialized after changing the systemd startup object. Before proceeding check the environment variable is correctly set through the use of the env command :
At this point the cluster can be initialized at a new location using initdb executed as root :
if successful the following output should be shown :
At this point the PostgreSQL cluster can be started. |
|||||||||||||||||||||
|
Migrating existing database from default location to a different location The configuration files, tablespaces and log files can be moved from the default location without data loss. This process requires downtime. The steps below will address the initialization of a new PostgreSQL cluster and the movement of a pre-existing one. Note:
The steps will need to be repeated when performing package upgrades as the default PostgreSQL settings will overwrite these changes.
The recommendation for a migration from the default location to a different one are to ensure the FlashArray volume has been formatted with a file system and mounted at the desired location. This example will assume that the new location is C:\PGSQL for Microsoft Windows and /pgsql for Linux operating systems.Both locations have a FlashArray volume mounted to them. Microsoft Windows This is disruptive and requires the PostgreSQL service to be stopped during this process. Any files should be copied and not moved during this process with the intention to preserve file integrity and existing permissions. Note:
Only the data directory is moved as apart of this process. Application binaries and files not stored in the data directory should not be moved as apart of this process.
Permissions for the destination storage volume should be the same as the source from where the data directory is being copied. In this example the data directory is found in C:\Program Files\PostgreSQL\13\ :
Viewing the permissions on the source data directory the DBTest@soln.local user has Full control of the folder:
Create a directory (in this example - data) in the root of the target filesystem and ensure it has the same permissions as the source directory :
In the Services Management Console identify the service responsible for PostgreSQL , right click on it and select Stop.
In a PowerShell terminal use the following command to copy the data folder and its contents to the intended destination :
The service used to automatically start the PostgreSQL cluster needs to be deleted and recreated with the correct command to target the new PostgreSQL data directory location. To avoid conflicts with the old data directory the existing service needs to be deleted : In the Services management dialog the PostgreSQL cluster in this example is managed by the postgresql-x64-13 service :
This service needs to be deleted , which can be done in a command terminal like PowerShell (As an Administrator enter the cmd process) with the command :
If successful the prompt should respond with the following message :
To recreate the service with the new settings use the following command in the same command terminal, adjusting the user (-U) and data directory (-D) as required :
This recreates the windows service with the correct data directory and user startup permissions.
Before starting the service for the first time the user must have the password entered for it , so that the service can start correctly. This can be done by going into the service properties and navigating to the Log On section. In the Log On section provide the password for the user account and select OK.
At this point the service can be started and will now use the new data directory location. Linux
The postgres user which is a member of the postgres group is created with the home directory : /var/lib/pgsql/ (also the default location for the data directory under the /version/ folder) during installation from a package or package manager. This user will be used to provide the correct permissions for the intended destination location. A FlashArray volume should be mounted and formatted with a file system before creating the folder and assigning the permissions. Before assigning the permissions ensure that a directory (in this example - data) has been created. This folder will contain the data directory contents. This can be done using the mkdir command as the postgres or root user :
This user and group need to be given ownership of /pgsql and its contents if the folders have been created by root or another user. The permissions can be assigned using the chown command :
In a terminal type the following command:
In a command terminal use the following command to copy the contents of the data directory to the intended destination:
The PGDATA environment variable needs to be changed so that it persists across sessions and is used correctly when starting the PostgreSQL cluster. For systemd based systems the service configuration file needs to be altered to ensure the new database directory is used. Persistently set PGDATA environment variables Note:
Note the PGDATA path in linux environments must not contain spaces. The PGDATA environment variable is set in the following locations and needs to be changed : ~/.bash_profile
(SystemD) Change database directory (As root) Identify the service file under which PostgreSQL is started in systemd (this is the .service file found after the Loaded section) :
Edit the service file , altering the Environment=PGDATA= line :
Save the file and reload SystemD :
(SELinux enabled systems only) - Change SELinux configuration to avoid service startup issues Get the SElinux labeling for the default data directory with the following command:
Once you have the label change it with the following command:
At this point the PostgreSQL cluster can be started and will use the new data directory location. |
|||||||||||||||||||||
|
Separating database components into isolated file systems This layout separates the storage objects for the cluster into separate volumes and file systems. This is useful for scenarios where quotas or more in depth reporting on the object types could be advantageous to database or storage administrators. The following guidelines can be used as a template for how each object could be implemented in a separate storage location :
For each physical location there should be a corresponding volume on FlashArray :
Microsoft Windows
With the volumes connected to the initiator ensure that there is a file system and appropriate volume label created on each.
Each volume with a file system should be mounted to the relevant mount point or provided with a distinct drive letter.
Once mounted with a drive letter or at a specific volume ensure that the user who will own the PostgreSQL installation and service has Full control of each filesystem.
To initialize a new PostgreSQL cluster the initdb application is used. The initdb application can be found in the installation directory for PostgreSQL (default location: C:\Program Files\PostgreSQL\<Version>) in the bin directory. It can then be executed with the -D argument to initialize the data directory at a new location (note do not use the root of a filesystem). Open PowerShell and execute the following , adjusting as required with additional arguments :
If everything is successful the output should be similar to the following :
The service used to automatically start the PostgreSQL cluster needs to be deleted and recreated with the correct command to target the new PostgreSQL data directory location. To avoid conflicts with the old data directory the existing service needs to be deleted : In the Services management dialog the PostgreSQL cluster in this example is managed by the postgresql-x64-13 service :
This service needs to be deleted , which can be done in a command terminal like PowerShell (As an Administrator enter the cmd process) with the command :
If successful the prompt should respond with the following message :
To recreate the service with the new settings use the following command in the same command terminal, adjusting the user (-U) and data directory (-D) as required :
This recreates the windows service with the correct data directory and user startup permissions.
Before starting the service for the first time the user must have the password entered for it , so that the service can start correctly. This can be done by going into the service properties and navigating to the Log On section. In the Log On section provide the password for the user account and select OK.
In the data directory there should now be a postgres.conf file conbtaining relevant configuration parameters , this is where continuous archiving and the log files can be set using similar syntax to the following : Continuous Archiving
Server Log file and rotation
Linux
Ensure that each multipath device has a filesystem created for it and is mounted at the relevant locations.
After each volume has been mounted in the relevant operating system it should be provided with the correct user permissions , for example in Linux the postgres user and postgres group should own the folder eacch volume is mounted to:
The PGDATA environment variable needs to be changed so that it persists across sessions and is used correctly when starting the PostgreSQL cluster. For systemd based systems the service configuration file needs to be altered to ensure the new database directory is used. Persistently set PGDATA environment variables Note:
Note the PGDATA path in linux environments must not contain spaces. The PGDATA environment variable is set in the following locations and needs to be changed : ~/.bash_profile
(SystemD) Change database directory (As root) Identify the service file under which PostgreSQL is started in systemd (this is the .service file found after the Loaded section) :
Edit the service file , altering the Environment=PGDATA= line :
Save the file and reload SystemD :
(SELinux enabled systems only) - Change SELinux configuration to avoid service startup issues Get the SElinux labeling for the default data directory with the following command:
Once you have the label change it with the following command:
The initdb application is apart of the postgresql-<version>-setup binary. This can be located in /usr/pgsql-<version>/bin. The data directory can only be initialized after changing the systemd startup object. Before proceeding check the environment variable is correctly set through the use of the env command :
At this point the cluster can be initialized at a new location using initdb executed as root :
if successful the following output should be shown :
In the data directory there should now be a postgres.conf file containing relevant configuration parameters , this is where continuous archiving and the log files can be set using similar syntax to the following : Continuous Archiving
Server Log file and rotation
|
|||||||||||||||||||||
|
Separating database storage locations This layout builds on any one of the previous architectures such as the #r_volume_and_file_system_architectural_layout_02__r_volume_and_file_system_architectural_layout_02-tbl1 , migrating existing database from default location to a different location or separating database components into isolated file systems. Separating databases and the objects associated with those databases by volume uses one or more tablespaces. Each tablespace is created on its own volume , and then each database is created within a specific table space. Note:
Tablespaces are cluster wide which means that multiple databases can be created on a single tablespace. This layout suggests that a ratio of 1 database for each tablespace be maintained to take full advantage of FlashArray QoS and reporting capabilities. This process can be performed with PostgreSQL online. The process of separating database storage locations requires that the required FlashArray volumes are mounted and have the correct permissions set before any further steps are taken. Once the volumes are mounted and have the appropriate permissions the only requirements are to create a tablespace on one or more volumes and then to create the required databases, specifying the tablespace to be one of those created on the FlashArray volumes. This example will create 4 databases , database01-04, on 4 separate tablespaces.
A total of 5 volumes are created on FlashArray :
These volumes are formatted with a file system and mounted in a directory with the correct permissions(The user who owns the PostgreSQL service) assigned to the mount point : Microsoft Windows
Linux
Using the CREATE TABLESPACE syntax the tablespace is created at a specific location. Once the tablespaces have been created using the /db+ command in psql the tablespaces and any associated properties should be viewable. Microsoft Windows Note:
For Microsoft Windows do not create the tablespace on the root of the volume , instead ensure there is a directory to contain the tablespace within that volume. Ensure the correct permissions are applied to that directory. Create tablespace example :
/db+ output :
Linux Create tablespace example :
/db+ output :
Using the CREATE DATABASE syntax with the TABLESPACE property and name allows the database to be created in the specific tablespace. Using the /l+ command in psql should show the tablespace that the database is a member of. Create database example :
/l+ output post database creation :
|
|||||||||||||||||||||