Postgis-to-kml is a project mainly written in ..., based on the View license.
Simple scripts using postgis, python and php
You can use both python and php scripts to generate KML
You have some sql queries samples in postgis_kml_attributes.sql
You only need PostGIS and PHP or Python with this scripts.
For licence, see LICENCE.txt
If you can install OGR/GDAL (http://www.gdal.org/), a short way to do the same can be ogr2ogr -f KML output.kml PG:'host=localhost dbname=france user=user password=pass' -sql "SELECT * from departement" Further about KML in OGR/GDAL http://www.gdal.org/ogr/drv_kml.html
You can for example embed the previous line with parameters in a python script using Subprocess http://docs.python.org/library/subprocess.html or start to use gdal python for custom needs.
For PHP, you can use embed Exec command http://php.net/manual/fr/function.exec.php