The problem
When you install with apt-get you will see a question that asks (scroll down to see the solution):
Restart services during package upgrades without asking? if(typeof __ez_fad_position != 'undefined'){__ez_fad_position('div-gpt-ad-tutorials_technology-medrectangle-3-0')}; Solution ========
To avoid any prompt at all use the variable DEBIAN_FRONTEND=noninteractive:
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y libpq-dev