When you install multiple PostgreSQL versions via apt-get on Ubuntu, the system defaults to using the older version's binaries. This occurs because:
- The package manager prioritizes stable repository packages
- Default PATH settings point to the original installation
- Symbolic links aren't automatically updated for newer versions
First verify which versions are actually installed:
dpkg -l | grep postgresql
And locate the binaries:
which psql
/usr/bin/psql --version
Ubuntu's update-alternatives
system provides the cleanest solution:
sudo update-alternatives --config psql
sudo update-alternatives --config pg_dump
sudo update-alternatives --config createdb
You'll see output like:
There are 2 choices for the alternative psql (providing /usr/bin/psql).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/psql-8.4 100 auto mode
1 /usr/bin/psql-8.4 100 manual mode
2 /usr/bin/psql-9.0 90 manual mode
Select the number corresponding to PostgreSQL 9.0.
For more control, prepend the newer version's bin directory to your PATH:
export PATH=/usr/lib/postgresql/9.0/bin:$PATH
Make this permanent by adding to ~/.bashrc
:
echo 'export PATH=/usr/lib/postgresql/9.0/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Confirm the change took effect:
psql --version
createdb --version
Both should now report 9.0.x.
Remember that switching CLI tools doesn't affect running services. To manage services:
sudo service postgresql stop
sudo service postgresql start 9.0
If you encounter problems:
- Connection refused: Check
pg_hba.conf
and postgresql.conf - Version mismatch: Ensure client and server versions match
- Port conflicts: Different versions should run on different ports
For clarity, here's a working 9.0 setup where 8.4 was default:
# In /etc/postgresql/9.0/main/postgresql.conf
port = 5433 # Different from 8.4's 5432
# In ~/.bash_profile
alias psql9='PGPORT=5433 psql -h localhost'
When you install multiple PostgreSQL versions via apt-get on Ubuntu, the system defaults to using the older version's CLI tools (like createdb
and psql
) due to how Linux prioritizes paths in $PATH
. Here's how Ubuntu typically structures the installations:
# Default 8.4 installation
/usr/lib/postgresql/8.4/bin/psql
# PPA-installed 9.0
/usr/lib/postgresql/9.0/bin/psql
The most straightforward solution is to use absolute paths:
/usr/lib/postgresql/9.0/bin/psql -U postgres
/usr/lib/postgresql/9.0/bin/createdb my_new_db
Update your .bashrc
or .zshrc
to prioritize PostgreSQL 9.0 binaries:
# Add this to ~/.bashrc
export PATH=/usr/lib/postgresql/9.0/bin:$PATH
Then reload your shell:
source ~/.bashrc
Create distinct aliases for each version in your shell configuration:
alias psql84='/usr/lib/postgresql/8.4/bin/psql'
alias psql90='/usr/lib/postgresql/9.0/bin/psql'
alias createdb90='/usr/lib/postgresql/9.0/bin/createdb'
After making changes, confirm which version is active:
psql --version
# Should return: psql (PostgreSQL) 9.0.x
Ubuntu's pg_wrapper system lets you handle multiple clusters:
# List available clusters
pg_lsclusters
# Start specific version cluster
sudo pg_ctlcluster 9.0 main start
# Connect to 9.0 cluster
psql --cluster 9.0/main
If you encounter port conflicts (both versions defaulting to 5432), modify postgresql.conf:
# For 9.0 installation
sudo nano /etc/postgresql/9.0/main/postgresql.conf
# Change port = 5433