|
| 1 | +--- |
| 2 | +title: How to install pg_dump on your Mac, Ubuntu, CentOS, Windows |
| 3 | +--- |
| 4 | + |
| 5 | +`pg_dump` is part of the PostgreSQL client utilities and doesn't come as a standalone tool. This guide covers how to install PostgreSQL client tools (including `pg_dump`) on various operating systems. |
| 6 | + |
| 7 | +## Mac OS |
| 8 | + |
| 9 | +### Method 1: Using Homebrew (Recommended) |
| 10 | + |
| 11 | +1. Install Homebrew if you don't have it: |
| 12 | + |
| 13 | + ```bash |
| 14 | + /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" |
| 15 | + ``` |
| 16 | + |
| 17 | +1. Install PostgreSQL: |
| 18 | + |
| 19 | + ```bash |
| 20 | + brew install postgresql |
| 21 | + ``` |
| 22 | + |
| 23 | +1. Verify installation: |
| 24 | + ```bash |
| 25 | + pg_dump --version |
| 26 | + ``` |
| 27 | + |
| 28 | +### Method 2: Using Postgres.app |
| 29 | + |
| 30 | +1. Download [Postgres.app](https://postgresapp.com/) |
| 31 | +1. Move to Applications folder and open |
| 32 | +1. Add to your PATH: |
| 33 | + ```bash |
| 34 | + sudo mkdir -p /etc/paths.d && echo /Applications/Postgres.app/Contents/Versions/latest/bin | sudo tee /etc/paths.d/postgresapp |
| 35 | + ``` |
| 36 | +1. Restart your terminal |
| 37 | +1. Verify installation: |
| 38 | + ```bash |
| 39 | + pg_dump --version |
| 40 | + ``` |
| 41 | + |
| 42 | +## Ubuntu |
| 43 | + |
| 44 | +1. Update package lists: |
| 45 | + |
| 46 | + ```bash |
| 47 | + sudo apt update |
| 48 | + ``` |
| 49 | + |
| 50 | +1. Install PostgreSQL client: |
| 51 | + |
| 52 | + ```bash |
| 53 | + sudo apt install postgresql-client |
| 54 | + ``` |
| 55 | + |
| 56 | +1. Verify installation: |
| 57 | + |
| 58 | + ```bash |
| 59 | + pg_dump --version |
| 60 | + ``` |
| 61 | + |
| 62 | + To install a specific PostgreSQL version (e.g., PostgreSQL 14): |
| 63 | + |
| 64 | + ```bash |
| 65 | + sudo apt install postgresql-client-14 |
| 66 | + ``` |
| 67 | + |
| 68 | +## CentOS/RHEL |
| 69 | + |
| 70 | +### For CentOS/RHEL 7.x |
| 71 | + |
| 72 | +1. Add PostgreSQL official repository: |
| 73 | + |
| 74 | + ```bash |
| 75 | + sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm |
| 76 | + ``` |
| 77 | + |
| 78 | +1. Install PostgreSQL client: |
| 79 | + |
| 80 | + ```bash |
| 81 | + sudo yum install -y postgresql14 |
| 82 | + ``` |
| 83 | + |
| 84 | +1. Verify installation: |
| 85 | + ```bash |
| 86 | + pg_dump --version |
| 87 | + ``` |
| 88 | + |
| 89 | +### For CentOS/RHEL 8.x and above |
| 90 | + |
| 91 | +1. Add PostgreSQL official repository: |
| 92 | + |
| 93 | + ```bash |
| 94 | + sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm |
| 95 | + ``` |
| 96 | + |
| 97 | +1. Disable built-in PostgreSQL module: |
| 98 | + |
| 99 | + ```bash |
| 100 | + sudo dnf -qy module disable postgresql |
| 101 | + ``` |
| 102 | + |
| 103 | +1. Install PostgreSQL client: |
| 104 | + |
| 105 | + ```bash |
| 106 | + sudo dnf install -y postgresql14 |
| 107 | + ``` |
| 108 | + |
| 109 | +1. Verify installation: |
| 110 | + ```bash |
| 111 | + pg_dump --version |
| 112 | + ``` |
| 113 | + |
| 114 | +## Windows |
| 115 | + |
| 116 | +### Method 1: Full PostgreSQL Installation |
| 117 | + |
| 118 | +1. Download the PostgreSQL installer from https://www.postgresql.org/download/windows/ |
| 119 | +1. Run the installer and follow the setup wizard |
| 120 | +1. Deselect components you don't need (e.g., pgAdmin, StackBuilder) |
| 121 | +1. Complete the installation |
| 122 | +1. Add PostgreSQL bin directory to your PATH: |
| 123 | + - Go to Control Panel > System and Security > System > Advanced system settings |
| 124 | + - Click "Environment Variables" |
| 125 | + - Edit PATH variable and add: `C:\Program Files\PostgreSQL\<version>\bin` |
| 126 | +1. Open a new Command Prompt and verify: |
| 127 | + ```cmd |
| 128 | + pg_dump --version |
| 129 | + ``` |
| 130 | + |
| 131 | +### Method 2: Using the ZIP Version (Client-Only) |
| 132 | + |
| 133 | +1. Download the ZIP archive from https://www.enterprisedb.com/download-postgresql-binaries |
| 134 | +1. Extract to a location (e.g., `C:\pgsql`) |
| 135 | +1. Add bin directory to PATH: |
| 136 | + - Go to Control Panel > System and Security > System > Advanced system settings |
| 137 | + - Click "Environment Variables" |
| 138 | + - Edit PATH variable and add: `C:\pgsql\bin` |
| 139 | +1. Open a new Command Prompt and verify: |
| 140 | + ```cmd |
| 141 | + pg_dump --version |
| 142 | + ``` |
| 143 | + |
| 144 | +## Using pg_dump |
| 145 | + |
| 146 | +Basic usage: |
| 147 | + |
| 148 | +```bash |
| 149 | +pg_dump -h hostname -p port -U username -d dbname -f output.sql |
| 150 | +``` |
| 151 | + |
| 152 | +Examples: |
| 153 | + |
| 154 | +```bash |
| 155 | +# Dump in plain SQL format |
| 156 | +pg_dump -h localhost -U postgres -d mydb -f backup.sql |
| 157 | + |
| 158 | +# Dump in custom format (compressed) |
| 159 | +pg_dump -h localhost -U postgres -d mydb -F c -f backup.dump |
| 160 | + |
| 161 | +# Dump schema only |
| 162 | +pg_dump -h localhost -U postgres -d mydb --schema-only -f schema.sql |
| 163 | + |
| 164 | +# Dump specific tables |
| 165 | +pg_dump -h localhost -U postgres -d mydb -t table1 -t table2 -f tables.sql |
| 166 | +``` |
| 167 | + |
| 168 | +## Troubleshooting |
| 169 | + |
| 170 | +### Common Issues |
| 171 | + |
| 172 | +1. **Command not found**: Ensure PostgreSQL bin directory is in your PATH |
| 173 | +2. **Permission denied**: Ensure you have proper permissions to run the command |
| 174 | +3. **Connection refused**: Check hostname, port, and ensure PostgreSQL server is running |
| 175 | +4. **Authentication failed**: Verify username and password |
| 176 | + |
| 177 | +### Environment Variables |
| 178 | + |
| 179 | +Setting up environment variables can make using pg_dump easier: |
| 180 | + |
| 181 | +```bash |
| 182 | +export PGHOST=localhost |
| 183 | +export PGPORT=5432 |
| 184 | +export PGUSER=postgres |
| 185 | +export PGPASSWORD=yourpassword # Not recommended for security reasons |
| 186 | +``` |
| 187 | + |
| 188 | +For Windows: |
| 189 | + |
| 190 | +```cmd |
| 191 | +set PGHOST=localhost |
| 192 | +set PGPORT=5432 |
| 193 | +set PGUSER=postgres |
| 194 | +set PGPASSWORD=yourpassword # Not recommended for security reasons |
| 195 | +``` |
0 commit comments