This question is similar to an earlier question about displaying vector data in a web-map. I want to have a basic web interface which uses OpenLayers and can display raster data that is currently in PostGIS, using a basemap such as google. I have installed PostGIS2.0 with the raster library and see from the documentation that there are several options to output data, such as JPEG, GeoTIFF and PNG. Is their a recommend format to use?
I have seen a lot of references to GeoServer but I'm not sure if it is necessary for this project (I'm not entirely sure when GeoServer should be used or not).
Rasters are images, so the best format will be an image format (png/jpg/etc). Geojson is a good format for vectors. You will never see, say, your facebook profile pic be transported back as pure json. Of course, returning a url to an image as json is very very different :)
As far as the jpeg vs png vs any other image format discussion, there are tons of different opinions about it. What it boils down to is that some image formats are lossless while others are lossy. Your decision depends on what you are going to do with the data. For analysis you may probably not tolerate lossy, but for displaying things on the web you would.
Can PostGIS 2.0 generate images? Sure. Look at the Raster output section.
Is this a good idea? It really really really depends on what you are doing. Sometimes generating images may take a really really really long time, and you want to cache them to avoid paying the price of generating them every single time you request them through a web environment. The only "caching" you can do on the PostGIS side is to save the results to a different table and then query the table instead when you need it. Geoserver has an entire infrastructure (GeoWebCache) for doing this.
Do you need GeoServer, probably not - but it makes life a lot easier when you have this middleware that exposes all of your data using OGC web services, does caching, styling of vectors and rasters a whole bunch more things.
Mapserver has support for postgis rasters and directions for a basic setup are given here (item 9.9).
In your case you would setup Mapserver to work as a WMS server so that it returns the Postgis rasters as normal WMS layers. It is then almost trivial to add the layers into your OpenLayers map by using a WMS layer object.
This kind of solution is probably your best option because it is all based on OpenSource software, it's a proven stack and does not require extensive middleware. All mapserver needs is a webserver (the apache you are probably already using for php is ok) that can run cgi scripts. If performance is not a critical issue then you can avoid using a (tile)cache and just have mapserver render the layers on the fly every time.
If you want to cache the tiles to minimize server load and improve response times consider using mapcache or tilecache. Mapcache is a younger sister project of Mapserver, but should outperform all other caches as it runs as an apache module.
For Ubuntu users the easiest easy way to install Mapserver's dependencies is to add the Ubuntu GIS PPA to your system.
It all depends. Image size, caching, available libraries (some proprietary jp2k drivers for example).
Here is a link to an awesome pdf, Geoserver on steroids. This will run you through the various formats, their pros and cons and some basic setup.