Postgresql – Best Approach to storing chat history

database-designdatabase-recommendationmongodbpostgresql

I'm building a chat application (Yes, another one :P) and am really curious about the best way to store a chat's history(String username, String Message, Time, and possibly string channel depending on how I end up setting it up.

Since I'm using PostgreSQL anyway, I was tempted for going with it for chat history. However, I don't see a sane approach for using it to do this without generating a new row for every message, something obviously not very scale-able.

I know some MongoDB and was thinking that using a json array for each channel and storing the history in that would work well.

Is using two entirely databases in the same application a terrible idea?

Are there any other alternatives I should take a look at? (I'm open to pretty much anything)

Is there a popular PostgreSQL way of doing this? I took a look at a JSON columns, but it seemed fairly new and limited at the moment.

Best Answer

You can use hstore for use EAV. hstore is available from pgsql 8.4. If you have installed json you can then use function hstore_to_json(hstore)

select hstore_to_json('"a key"=>1, b=>t, c=>null, d=>12345, e=>012345, f=>1.234, g=>2.345e+4')

this will output:

{"a key": "1", "b": "t", "c": null, "d": "12345", "e": "012345", "f": "1.234", "g": "2.345e+4"}