Cyberithub

How to Install pg_dump and pg_restore on macOS Using 7 Easy Steps

Advertisements

In this article, we will see how to install pg_dump and pg_restore on macOS Using 7 Easy Steps. If you are using Postgresql database and planning to take the dump of the DB then there is no better tool than pg_dump to use here. Similarly, for restoring the database there is no better tool than pg_restore utility to perform the task. Both of the tools are very simple to use and handle. It provides the flexibility of using multiple options while taking the backup or during the restoration.

pg_dump are being consistently used by database administrators to backup the data while users are still connected to the database. Then pg_restore can be used to restore data from an archive created by pg_dump. It is also very easy to install both utilities in almost all the famous Linux and Unix systems. Here we are going to look into the steps to install pg_dump and pg_restore utilities on macOS in great detail with the help of a real world example.

Advertisements

 

How to Install pg_dump and pg_restore on macOS Using 7 Easy Steps

How to Install pg_dump and pg_restore on macOS Using 7 Easy Steps

Also Read: How to Find the Serial Number on a MacBook Using 2 Easy Methods

Step 1: Prerequisites

a) You should have a running macOS System.

Advertisements

b) You should have brew utility available in your System.

c) You should have access to install formulae in your System.

Advertisements

 

Step 2: Update Your Server

In the first step, you need to update all the outdated formulae by using brew update command as shown below.

[cyberithub@macos1066 ~ % brew update

 

Step 3: Install pg_dump and pg_restore

Since in macOS, pg_dump and pg_restore are available through libpq formulae, so in order to install both the utilities you need to run brew install libpq command as shown below. This will download and install the formulae along with all its dependencies.

Advertisements
[cyberithub@macos1066 ~ % brew install libpq
==> Downloading https://formulae.brew.sh/api/formula.jws.json
############################################################################################################################## 100.0%
==> Downloading https://formulae.brew.sh/api/cask.jws.json
############################################################################################################################## 100.0%
==> Fetching dependencies for libpq: krb5
==> Fetching krb5
==> Downloading https://ghcr.io/v2/homebrew/core/krb5/manifests/1.20.1
############################################################################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/krb5/blobs/sha256:c52hbdfef3r7r5959774hbjhr8085brjgnhy55i565gnher9
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:c52hbdfef3r7r5959774hbjhr8085brjgnhy55i565gnher9?se=2023-05-15T12%3A40%3A00Z&sig=%2F2ac%2B0sSw
############################################################################################################################## 100.0%
==> Fetching libpq
==> Downloading https://ghcr.io/v2/homebrew/core/libpq/manifests/15.3
...................................................

 

Step 4: Export PATH

If you have PostgreSQL already installed in your system and you need to have libpq first in your $PATH then you need to run export PATH="/usr/local/opt/libpq/bin:$PATH" command to detect pg_dump and pg_restore utility and avoid any conflict as shown below.

[cyberithub@macos1066 ~ % export PATH="/usr/local/opt/libpq/bin:$PATH"

It is also important to understand that above export command will temporarily export the path in $PATH environment variable. To make the changes permanent, you need to add the path in ~/.zshrc if you are using zsh environment, ~/.bash_profile if you are using bash environment or in /etc/paths depending on the environment you are using.

 

Step 5: Check Version

To check the installed version of pg_dump, you can run pg_dump --version command as shown below.

[cyberithub@macos1066 ~ % pg_dump --version

Similarly, to check the installed version of pg_restore, you can run pg_restore --version command as shown below.

[cyberithub@macos1066 ~ % pg_restore --version

 

Step 6: Using pg_dump and pg_restore

There are multiple ways to backup and restore PostgreSQL database. You can either login to database server and take backup locally or you can connect remotely to the DB Server and take backup in certain directory in various output formats such as tar, dump or in plain text SQL. For example - if you need to take backup of a database called cyberithub_db locally into the database server then you need to first login to the database using postgres account and run below command to take the dump in plain text SQL file.

[postgres@macos1066 ~ % pg_dump cyberithub_db > cyberithub_db.sql

You can also take the dump in tar format by specifying t option with -F switch as shown below.

[postgres@macos1066 ~ % pg_dump -F t cyberithub_db > cyberithub_db.tar

Similarly, if you are looking to take the backup in dump format then you need to specify c with -F switch as shown below.

[postgres@macos1066 ~ % pg_dump -F c cyberithub_db > cyberithub_db.dump

Likewise, if you are planning to take backup by remotely connecting database server of IP 200.128.16.10 on default Port 5432 then you need to run pg_dump -U postgres -h 200.128.16.10 -p 5432 cyberithub_db > cyberithub_db.sql command as shown below.

[cyberithub@macos1066 ~ % pg_dump -U postgres -h 200.128.16.10 -p 5432 cyberithub_db > cyberithub_db.sql

You can also take backup in some directory say example directory in our case by using below command.

[postgres@macos1066 ~ % pg_dump -F d cyberithub_db -f example

If you are looking to restore the database from the dump which you have taken through pg_dump then you need to use pg_restore -d cyberithub_db cyberithub_db.dump command as shown below.

[postgres@macos1066 ~ % pg_restore -d cyberithub_db cyberithub_db.dump

Similarly, you can restore from tar dump using pg_restore -d cyberithub_db cyberithub_db.tar command as shown below.

[postgres@macos1066 ~ % pg_restore -d cyberithub_db cyberithub_db.tar

If you saved your backup in some directory called example then you can restore the database from the directory by using pg_restore -d cyberithub_db example command as shown below.

[postgres@macos1066 ~ % pg_restore -d cyberithub_db example

 

Step 7: Uninstall pg_dump and pg_restore

Once you are done using pg_dump and pg_restore utility then you can choose to uninstall it from your mac system by using brew remove libpq command as shown below. However it is important to note here that below command does not remove the installed dependencies. To remove those, you need to identify and uninstall them manually by yourself using same brew remove command.

[cyberithub@macos1066 ~ % brew remove libpq
Uninstalling /usr/local/Cellar/libpq/15.3... (2,369 files, 28.2MB)

Leave a Comment