How to Disable Autostart for Specific PostgreSQL Version on Ubuntu While Keeping It Installed


14 views

When dealing with multiple PostgreSQL versions on Ubuntu, it's crucial to understand how the init system manages different versions. The postgresql service in /etc/init.d/ is actually a master script that controls all installed versions through symbolic links.

ls -l /etc/init.d/postgresql*
# Typical output:
# /etc/init.d/postgresql -> ../lib/postgresql-common/init.d-functions
# /etc/init.d/postgresql-9.1
# /etc/init.d/postgresql-9.3

To prevent PostgreSQL 9.1 from starting automatically while keeping it installed, use the update-rc.d command:

sudo update-rc.d postgresql-9.1 disable
# This creates these symlinks:
# /etc/rc*.d/KXXpostgresql-9.1 -> ../init.d/postgresql-9.1

If you're using systemd (Ubuntu 15.04+), the command would be:

sudo systemctl disable postgresql@9.1-main

After making changes, verify the new behavior:

# Check current status
sudo service postgresql-9.1 status

# Test the new boot configuration
sudo shutdown -r now
# After reboot:
pg_lsclusters

You can manually control specific clusters when needed:

# Start only when needed
sudo pg_ctlcluster 9.1 main start

# Stop when finished
sudo pg_ctlcluster 9.1 main stop

Unlike killing processes post-boot, this method:

  • Prevents the service from starting at all
  • Maintains clean service management
  • Allows for proper dependency handling during startup
  • Preserves the ability to manually start when needed

If you want to completely prevent accidental starts, you can also edit the PostgreSQL configuration:

# In /etc/postgresql/9.1/main/start.conf
# Change from 'auto' to:
manual

After upgrading from PostgreSQL 9.1 to 9.3 on Ubuntu 12.04, I found both versions automatically starting on boot. Here's what pg_lsclusters shows:

Ver Cluster Port Status Owner    Data directory               Log file
9.1 main    5433 online postgres /var/lib/postgresql/9.1/main /var/log/postgresql/postgresql-9.1-main.log
9.3 main    5432 online postgres /var/lib/postgresql/9.3/main /var/log/postgresql/postgresql-9.3-main.log

PostgreSQL on Ubuntu uses init.d scripts for service management. The main control script is /etc/init.d/postgresql, which handles all installed versions. The script reads configuration from /etc/postgresql-common/ to determine which clusters to start.

There are two clean ways to prevent PostgreSQL 9.1 from auto-starting:

Method 1: Using pg_ctlcluster

The recommended approach is to disable the specific cluster:

sudo pg_ctlcluster 9.1 main stop
sudo update-rc.d postgresql disable 9.1

This modifies the symlinks in /etc/rc*.d/ to prevent auto-start while keeping the installation intact.

Method 2: Manual Configuration Edit

Alternatively, edit the auto-start configuration:

sudo nano /etc/postgresql/9.1/main/start.conf

Change the line to read:

auto = false

After reboot, verify with:

pg_lsclusters

You should see 9.1 as "down" while 9.3 remains "online". To manually start 9.1 when needed:

sudo pg_ctlcluster 9.1 main start

For newer Ubuntu versions using systemd:

sudo systemctl disable postgresql@9.1-main
sudo systemctl stop postgresql@9.1-main

Unlike complete removal or kill scripts, this approach:

  • Preserves all 9.1 data and configuration
  • Maintains the ability to start 9.1 manually when needed
  • Properly integrates with Ubuntu's service management
  • Consumes zero resources when 9.1 isn't running