I successfully compiled and installed the latest version of PostgreSQL (17.4) on Ubuntu ARM32.
The commands in this article mainly come from the official PostgreSQL documentation. I have actually tested each command in this article.
You can see the systems that can be successfully installed here: PostgreSQL Build Farm
Compile and Install PostgreSQL 17.4#
1. Create a directory and enter it#
mkdir postgresql && cd postgresql
mkdir postgresql: Create a new directory namedpostgresql.cd postgresql: Enter the newly createdpostgresqldirectory.
2. Download PostgreSQL source code#
wget https://ftp.postgresql.org/pub/source/v17.4/postgresql-17.4.tar.bz2
wget: Download a file from the specified URL.https://..../postgresql-17.4.tar.bz2: The download link for the compressed source code package of PostgreSQL version 17.4. If the link is broken, you can search for a new link yourself.
3. Extract the source code package#
tar xjf postgresql-17.4.tar.bz2
-
tar: Used to handle.tarcompressed files. -
xjf: Options for extractingtarfiles:x: Extract files.j: Use bzip2 compression format (.bz2).f: Specify the file to extract.
-
postgresql-17.4.tar.bz2: The name of the file to extract.
4. Enter the extracted source code directory#
cd postgresql-17.4
- Enter the extracted PostgreSQL 17.4 source code directory.
5. Install dependencies#
sudo apt install libssl-dev libsystemd-dev libxml2-dev libreadline-dev
sudo apt install: Use theaptpackage manager to install packages.libssl-dev,libsystemd-dev,libxml2-dev,libreadline-dev: Install the development libraries required for compiling PostgreSQL, which are SSL library, Systemd library, XML library, and Readline library, respectively.
6. Configure compilation options#
./configure --prefix=/usr/local/pgsql --with-openssl --with-libxml --with-systemd
./configure: Prepare the compilation environment for PostgreSQL.--prefix=/usr/local/pgsql: Specify the installation directory for PostgreSQL as/usr/local/pgsql.--with-openssl: Enable OpenSSL support.--with-libxml: Enable XML support.--with-systemd: Enable Systemd support.
7. Compile the source code#
make -j$(nproc)
make: Start the compilation process.-j$(nproc): Usenproc(the number of CPU cores on the system) to execute compilation tasks in parallel, speeding up the compilation process.
8. Install PostgreSQL#
sudo make install
sudo make install: Execute the installation with administrator privileges usingsudo,make installwill install the compiled files into the system, specifically into the/usr/local/pgsqldirectory specified in the previous./configurecommand.
9. Set environment variables#
Using a text editor 1. Edit the /etc/profile file:
sudo nano /etc/profile
sudo: Indicates that the command is run with superuser (administrator) privileges. Modifying the/etc/profilefile requires administrator permissions.nano: This is a text editor commonly used in terminal environments to edit files./etc/profile: This is a global configuration file used to set system-wide environment variables and startup configurations. The settings in this file are executed for all users when they log into the system.
- Add environment variables:
export PATH=/usr/local/pgsql/bin:$PATH
export: Used to set an environment variable, making it available in the current and child processes.PATH: This is an environment variable that contains a list of directories for executable files in the system. When executing commands, the system searches for commands in the directories listed in thePATHvariable./usr/local/pgsql/bin: This is the directory for the executable files of the PostgreSQL database; adding it means that executable files (likepsql) in this directory will be recognized by the system.:$PATH: Here,$PATHrepresents the existing paths, adding the new path/usr/local/pgsql/binto the front of the existingPATH. This way, the system will check the new directory first before checking the original paths.
The purpose of this command is to add the PostgreSQL executable file directory to the system's environment variable PATH, ensuring that PostgreSQL commands (like psql) can be run directly from the command line without needing to provide the full path. The modified settings will take effect for all users.
Initialize PostgreSQL Database#
These commands are mainly used to manually configure the PostgreSQL database server on a Linux server, including creating a PostgreSQL system user, initializing the database, starting the database service, and registering it as a systemd service for automatic startup on boot.
1. Create PostgreSQL User#
sudo useradd -m -U -r -d /var/lib/postgresql -s /bin/bash postgres
sudo: Execute the command with superuser privileges.useradd: Create a new user.-m: Create the user's home directory.-U: Also create a user group with the same name as the username.-r: Create a system user (system user UID is less than 1000, not used for login).-d /var/lib/postgresql: Specify the home directory as/var/lib/postgresql.-s /bin/bash: Specify the shell as/bin/bash.postgres: Username.
This command creates a dedicated system user postgres for PostgreSQL.
2. Change Ownership of PostgreSQL Directory#
sudo chown postgres /usr/local/pgsql
chown postgres /usr/local/pgsql: Change the owner of the/usr/local/pgsqldirectory to thepostgresuser, allowing it to read and write to this directory.
3. Set Password for PostgreSQL User#
sudo passwd postgres
passwd postgres: Change the password for thepostgresuser.
4. Switch to PostgreSQL User#
su postgres
su postgres: Switch to thepostgresuser to use PostgreSQL-related commands.
5. Initialize Database#
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
initdb -D /usr/local/pgsql/data: Initialize the database storage in the/usr/local/pgsql/datadirectory.
6. Start PostgreSQL Database#
/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
postgres -D /usr/local/pgsql/data: Start the database with thepostgresprocess, storing data in the/usr/local/pgsql/datadirectory.
7. In another terminal session, enter the database#
su postgres
/usr/local/pgsql/bin/psql
psql: The command-line interactive tool for PostgreSQL, entering the database shell.
8. Set Password for Database User#
ALTER USER postgres WITH PASSWORD 'your_new_password';
- Change the password for the database user
postgres.
9. Create systemd Service#
sudo nano /etc/systemd/system/postgresql.service
nano /etc/systemd/system/postgresql.service: Edit thesystemdconfiguration file to allow PostgreSQL to run as a service.
Configuration file content:
[Unit]
Description=PostgreSQL server
#Documentation=man:postgres(1)
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
User=postgres
ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
KillSignal=SIGINT
TimeoutSec=infinity
[Install]
WantedBy=multi-user.target
Description=PostgreSQL server: Describes that this service is the PostgreSQL server.After=network-online.target: Wait for the network connection to be available before starting PostgreSQL.User=postgres: Run as thepostgresuser.ExecStart=/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data: Start the PostgreSQL server.ExecReload=/bin/kill -HUP $MAINPID: SendHUPsignal on reload.KillMode=mixed: Mixed termination mode.KillSignal=SIGINT: Signal to terminate the PostgreSQL process.TimeoutSec=infinity: Timeout set to infinite.
10. Enable systemd Service#
sudo systemctl daemon-reload
sudo systemctl start postgresql
sudo systemctl status postgresql
sudo systemctl enable postgresql
daemon-reload: Reloadsystemdconfiguration to make the newpostgresql.serviceeffective.start postgresql: Start the PostgreSQL service.status postgresql: Check the running status of PostgreSQL.enable postgresql: Enable PostgreSQL to start automatically on boot.
The purpose of these commands is:
- Create a dedicated
postgresuser for running PostgreSQL. - Initialize the database storage directory.
- Start the PostgreSQL database and enter the
psqlinteractive command line. - Configure
systemdto run PostgreSQL as a system service and support automatic startup.
This way, the PostgreSQL server can run normally and can automatically start on system boot.
References#
PostgreSQL: Documentation: 17: Chapter 17. Installation from Source Code
Other Versions of This Page#
This article is available in multiple languages.
If you would like to leave a comment, please visit the following page:
These pages are for browsing only and do not support comments or messages, but they offer more language options and load faster:
ZH EN ZH-TW JA RU KO CS ES AR FR PT DE TR IT NL SV DA FI PL UK HE RO HU EL HR TH HI BN ID SW VI NO