How to Install PSQL Without Installing Full PostgreSQL

Bilal Shahid Feb 02, 2024
  1. Install psql Without Installing Full PostgreSQL
  2. Install psql Without Installing the Complete PostgreSQL Server Package
How to Install PSQL Without Installing Full PostgreSQL

This article is specially articulated to discuss the procedure to install psql without installing the complete PostgreSQL server package.

Install psql Without Installing Full PostgreSQL

The procedure of installing psql without having full PostgreSQL does not include technical and complex commands. Instead, it is an easy-to-follow procedure that requires just a few minutes.

The procedure to use psql without installing the entire PostgreSQL server package requires the help of the Homebrew software, which is used to use psql without installing full Postgres.

Before moving towards installation, let’s understand the difference between psql and PostgreSQL below.

Difference Between psql and PostgreSQL Server

The PostgreSQL server is different from the psql. This section is specifically written to differentiate between the two terms beforehand to avoid confusion between these two terms throughout the article.

PostgreSQL is a server that runs independently from all the different client connections. The pg_ctl command or an init script starts and stops the server.

It is necessary to start the server before any client can connect. On the other hand, the psql is a command-line client. It is a terminal-based front-end to the PostgreSQL server.

The psql is connected to the server, enabling you to input queries, issue them to PostgreSQL, and see the results. You can input queries from a file or use command line arguments.

Either way, the queries are issues to the PostgreSQL server, and the result is shown to the user. Therefore, it must be clear that the PostgreSQL server and psql are two different things.

Install psql Without Installing the Complete PostgreSQL Server Package

Homebrew software has made it easy for users to use psql without installing the complete PostgreSQL package. Instead, only a few commands need to be executed to use psql along with the features of the PostgreSQL server.

The article includes two methods to use psql. The first method does not require the user to install the PostgreSQL server package. However, the second method requires the user to install the PostgreSQL server package to run at the back end.

Both procedures are discussed in detail to help you easily follow through with the process.

Solution 1: Use Homebrew Software to Install psql Without PostgreSQL Server

This solution uses the Homebrew software and does not require you to install the PostgreSQL server. The process of using psql without a PostgreSQL server is discussed below.

Firstly, install libpq using the Homebrew software. The following command can perform this task:

brew install libpq

The successful installation of libpq gives you psql, pg_dump, and several other client utilities. Installing the PostgreSQL server is not required to use these client utilities.

The user is provided with the same utilities as the ones included in the complete PostgreSQL server package. In addition, brew installs the client utilities as "keg-only".

The installed files are not linked to the shared bin, lib, and other directories. Instead, these binaries need to be linked and added to the path to use them.

Three different procedures to link the binaries are mentioned below.

  1. The first procedure to link the files is to update the path. We can do it using the commands mentioned below.

    • For zsh users:

      echo'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc
      source ~/.zshrc
      
    • For BASH users:

      echo'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile
      

    Remember, the path for the binaries may have been updated to echo 'export PATH="/opt/homebrew/opt/libpq/bin:$PATH"' >> ~/.zshrc.

  2. An alternative to updating the path is creating symlinks for any utility you require. An example of creating a symlink is as follows:

    ln -s /usr/local/Cellar/libpq/10.3/bin/psql /usr/local/bin/psql
    

    We should use the installed version instead of 10.3 in the command above.

  3. The last procedure that can be used to link the binaries to the path is to instruct Homebrew software to link all of its binaries to the path. For example, the following command executes this task:

    brew link --force libpq
    

    The issue with this procedure is that installing the PostgreSQL package would not be possible later. In addition, this command creates a lot of symlinks that you might not require.

Solution 2: Use Homebrew Software to Install psql With PostgreSQL Server

The second solution to get psql involves downloading the PostgreSQL server; however, the server is not used. Homebrew has a postgres command but not a psql command.

The PostgreSQL package must be installed using the Homebrew postgres command. Towards the bottom of the caveats section, we can see that this does not run the database.

Instead, it only puts the files on your system.

> brew install postgres 
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.6.5.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring postgresql-9.6.5.sierra.bottle.tar.gz
==>/usr/local/Cellar/postgresql/9.6.5/bin/initdb /usr/local/var/postgres 
==> Caveats 
<snip>To have launched, start postgresql now and restart at login:
brew services start postgresql 
Or, if you don't want/need a background service, you can just run: 
pg_ctl -D /usr/local/var/postgres start 
==> Summary 
🍺 /usr/local/Cellar/postgresql/9.6.5: 3,269 files, 36.7MB

The psql command can now connect to remote PostgreSQL servers. The local server would not be running on your system with this process.

It can also be verified by checking the installed Homebrew services:

> brew services list 
Name Status User Plist 
mysql stopped 
postgresql stopped

Homebrew services can also be accessed even if they are not installed. We can use the following command to get the functionality:

> brew tap homebrew/services

So, the first solution allows you to use psql without installing the complete PostgreSQL server package. On the other hand, the second solution will enable you to use psql after installing the PostgreSQL server.

However, the second solution allows you to connect with remote servers without running the local PostgreSQL server.

Author: Bilal Shahid
Bilal Shahid avatar Bilal Shahid avatar

Hello, I am Bilal, a research enthusiast who tends to break and make code from scratch. I dwell deep into the latest issues faced by the developer community and provide answers and different solutions. Apart from that, I am just another normal developer with a laptop, a mug of coffee, some biscuits and a thick spectacle!

GitHub