Une erreur s'est produite.

L'erreur a été envoyée par e-mail à l'administrateur pour analyse.

How do I install the latest version of PostgreSQL.

Install PostgreSQL from the source code.

Hello, my name is Christopher Tate. I am a Red Hat consultant and I love open source software. When backed by a database, your website does not need to be static content only. You can create users with roles to add content and customize the site. The best part is, the very best database on the planet is PostgreSQL, and it's open source. I will help you install the most recent version of PostgreSQL to develop your website the open source way.

How do I install the dependencies for PostgreSQL?

Build the dependencies with yum.

sudo yum-builddep -y postgresql
  1. sudo: The command to install yum repositories begins with the command sudo. The "sudo" command allows your current user to have root privileges for a few minutes after entering your password.
  2. yum Stands for "Yellowdog Updater Modified", but nobody knows it as yellowdog, just yum. yum-builddep installs missing dependencies for building an RPM package.
  3. -y: Suppress messages asking to confirm if you want to install the software.
  4. postgresql: The package name you wish to install the dependencies for.
sudo yum install -y gcc systemd-devel docbook-dtds docbook-simple docbook-slides docbook-stype-dsssl docbook-style-xsl docbook-utils docbook-utils-pdf
  1. sudo
  2. yum
  3. install: For installing new yum packages.
  4. -y
  5. gcc: GNU project C and C++ compiler.
  6. systemd-devel: Development headers and auxiliary files for developing applications for systemd.
  7. docbook-*: DocBook is a syntax of Linux technical documentation texts (articles, books and manual pages).

Where do I put the PostgreSQL source code?

Create a pgsql directory in /usr/local/src.

sudo install -d -o $USER -g $USER /usr/local/src/pgsql
  1. sudo
  2. install: Creates directories and sets attributes on the new directory.
  3. -d: Create directories.
  4. -o: Set ownership on the directory (super-user only).
  5. $USER: The current user to make the owner of the directory.
  6. -g: Set group ownership on the directory (super-user only).
  7. $USER: The current user to make the group owner of the directory.
  8. /usr/local is a place for extra software that you install without the package manager. /usr/local/src is a good place to clone your open source software repositories.

Where do I install PostgreSQL?

Create a pgsql directory in /opt.

sudo install -d -o $USER -g $USER /opt/pgsql
  1. sudo
  2. install
  3. -d
  4. -o
  5. $USER
  6. -g
  7. $USER
  8. /opt is a good place to install open source software applications like PostgreSQL.

How do I download the PostgreSQL source code?

Clone the source code with the git command.

git clone git://git.postgresql.org/git/postgresql.git /usr/local/src/pgsql
  1. git: The distributed revision control system for most open source software.
  2. clone: Clone a repository into a directory.
  3. git://git.postgresql.org/git/postgresql.git: The git URL to the PostgreSQL source code repository.
  4. /usr/local/src/pgsql: The directory where the PostgreSQL source code will be cloned.

How do I pick a recent version of PostgreSQL?

List all the tags in the git repository.

(cd /usr/local/src/pgsql && git tag)
  1. (cd: Change directory temporarily, because of the open parenthesis.
  2. /usr/local/src/pgsql: The directory where the PostgreSQL source code was cloned.
  3. &&: Run the cd and git command together.
  4. git
  5. tag): Lists the current tags or assigned versions of the code and close the parenthesis.

How do I switch versions of the PostgreSQL source code?

Checkout the git tag.

(cd /usr/local/src/pgsql && git checkout REL_11_3)
  1. (cd
  2. /usr/local/src/pgsql
  3. &&
  4. git
  5. checkout: Checkout a branch to the working tree.
  6. REL_11_3): The latest tag to base the branch from.

How do I build the PostgreSQL source code?

Build PostgreSQL with the configure command.

(cd /usr/local/src/pgsql && ./configure --with-openssl --with-libxml --with-libxslt --with-systemd --with-selinux --prefix=/opt/pgsql)
  1. (cd
  2. /usr/local/src/pgsql
  3. &&
  4. ./configure: A C++ configure script to compile PostgreSQL.
  5. --with-openssl: Build PostgreSQL with OpenSSL support for secure TLS connections to the database.
  6. --with-libxml: Build PostgreSQL with XML support.
  7. --with-libxslt: Use XSLT support when building contrib/xml2.
  8. --with-systemd: Build PostgreSQL with support for the systemd service manager for Linux.
  9. --with-selinux: Build PostgreSQL with extra Security-Enhanced Linux features to provide an additional layer of security. Each table or function access initiated by a DML query will be checked against the system security policy.
  10. --prefix=/opt/pgsql): The prefix is the directory where several other subdirectories will be created with PostgreSQL files. /opt/pgsql/bin will contain PostgreSQL binaries. /opt/pgsql/include will contain PostgreSQL headers. /opt/pgsql/lib will contain PostgreSQL shared libraries. /opt/pgsql/share will contain PostgreSQL documentation and other files.
(cd /usr/local/src/pgsql && make world)
  1. (cd
  2. /usr/local/src/pgsql
  3. &&
  4. make: Compile the PostgreSQL source code.
  5. world): To build everything that can be built, including the documentation (HTML and man pages), and the additional modules (contrib).
(cd /usr/local/src/pgsql && make install-world)
  1. (cd
  2. /usr/local/src/pgsql
  3. &&
  4. make
  5. install-world): By building the world above, this installs everything, including the documentation.

After installing PostgreSQL, where is my data directory?

Create a pgsql directory in /srv.

sudo install -d -o $USER -g $USER /srv/pgsql
  1. sudo
  2. install
  3. -d
  4. -o
  5. $USER
  6. -g
  7. $USER
  8. /srv is a good place to install open source software serveurs like PostgreSQL that run as a service.

How do I create a new PostgreSQL database?

How to initialize a new PostgreSQL server.

Even after you compiled the PostgreSQL source code, you don't yet have a PostgreSQL database. You only have PostgreSQL binaries and tools. You need to setup a PostgreSQL database server to run and store data.

/opt/pgsql/bin/initdb -D /srv/pgsql -W
  1. /opt/pgsql/bin/initdb: Initializes a PostgreSQL database cluster.
  2. checkout: Choose a directory for the new PostgreSQL database server.
  3. /srv/pgsql: The directory of the new PostgreSQL database server.
  4. -W: Prompt for a password for the new superuser.

How do I run PostgreSQL all the time, when my computer starts up?

How to run PostgreSQL as a systemd service.

echo '
[Unit]

Description=PostgreSQL database server.

After=syslog.target network.target

[Service]
Type=forking
Environment=PGDATA=/srv/pgsql PGPORT=5432
User='"$USER"'
Group='"$USER"'
ExecStart=/opt/pgsql/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300
ExecStop=/opt/pgsql/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/opt/pgsql/bin/pg_ctl reload -D ${PGDATA} -s
TimeoutSec=300

[Install]
WantedBy=multi-user.target
' | sudo tee /usr/lib/systemd/system/pgsql.service
  1. echo '...': Write a string to the console.
  2. [Unit]: A unit configuration file whose name ends in ".service" encodes information about a process controlled and supervised by systemd.
  3. Description=...: The description of the service.
  4. After=...: The PostgreSQL service will be started after the following dependencies.
  5. [Service]: Information about a service controlled and supervised by systemd.
  6. Type=forking: Because it is expected that the process configured with ExecStart= will call fork() as part of its start-up.
  7. Environment=PGDATA=/srv/pgsql PGPORT=5432: To set the PostgreSQL data directory and port as system environment variables to use in later commands.
  8. User=...: The user as the current user that will run the service.
  9. Group=...: The group user as the current user that will run the service.
  10. ExecStart=...: The PostgreSQL command that will start the service.
  11. ExecStop=...: The PostgreSQL command that will stop the service.
  12. ExecReload=...: The PostgreSQL command that will restart the service.
  13. TimeoutSec=300: The number of seconds before giving up starting the service.
  14. [Install]: This section is optional and is used to define the behavior or a unit if it is enabled or disabled.
  15. WantedBy=multi-user.target: When the service is enabled, a directory called multi-user.target.wants will be created within /etc/systemd/system (if not already available) and a symbolic link to the current unit will be placed within.
  16. |: Read the string into the following command.
  17. sudo
  18. tee: Write a string to a file.
  19. /usr/lib/systemd/system/pgsql.service: A new systemd service file to write to for PostgreSQL.

How does the system know about the new service?

Reload the systemd daemon.

sudo systemctl daemon-reload
  1. sudo
  2. systemctl: Controls the systemd system and service manager.
  3. daemon-reload: Reload the configuration file of a systemd unit.

How do I enable the PostgreSQL service?

Use the systemctl enable command.

sudo systemctl enable pgsql
  1. sudo
  2. systemctl
  3. enable: Enable one or more unit files or unit file instances.
  4. pgsql: Enable the PostgreSQL service to start when the computer starts.

How do I start the PostgreSQL service?

Use the systemctl start command.

sudo systemctl start pgsql
  1. sudo
  2. systemctl
  3. start: Start one or more services.
  4. pgsql: Start the PostgreSQL service.

How do I make sure the PostgreSQL service started successfully?

Use the systemctl status command.

sudo systemctl status pgsql
  1. sudo
  2. systemctl
  3. status: Check the status of one or more services.
  4. pgsql: Check the status of the PostgreSQL service.

Now enjoy that latest and greatest open source database server of yours.

To review:

Thursday
May 16 2019 How do I install the latest version of PostgreSQL. Install PostgreSQL from the source code. By Christopher Tate
Questions Answers
How do I install the dependencies for PostgreSQL? Build the dependencies with yum.
Where do I put the PostgreSQL source code? Create a PostgreSQL directory in /usr/local/src.
Where do I install PostgreSQL? Create a PostgreSQL directory in /opt.
How do I download the PostgreSQL source code? Clone the source code with the git command.
How do I pick a recent version of PostgreSQL? List all the tags in the git repository.
How do I switch versions of the PostgreSQL source code? Checkout the git tag.
How do I build the PostgreSQL source code? Build PostgreSQL with the configure command.
After installing PostgreSQL, where is my data directory? Create a pgsql directory in /srv.
How do I create a new PostgreSQL database? How to initialize a new PostgreSQL server.
How do I run PostgreSQL all the time, when my computer starts up? How to run PostgreSQL as a systemd service.
How does the system know about the new service? Reload the systemd daemon.
How do I enable the PostgreSQL service? Use the systemctl enable command.
How do I start the PostgreSQL service? Use the systemctl start command.
How do I make sure the PostgreSQL service started successfully? Use the systemctl status command.

Previous and next articles.

Don't give up on your dreams. You can do hard things!

Share this story.

Let's get connected.
Up to the top.