0

I have a Postgres DB that is used by a chat application. The chat system often truncates these tables when they grow to big but I need this data copied to another Postgres database. I will not be truncating the tables in this DB.

How I can configure a few tables on the chat-system's database to replicate data to another Postgres database. Is there a quick way to accomplish this?

2
  • Did you consider partitioning the table? And why do you need to copy it to another DB? Wouldn't a different Schema be enough? Commented Nov 29, 2011 at 14:45
  • Perhaps you could use dblink and an AFTER INSERT-trigger? Commented Nov 29, 2011 at 14:48

2 Answers 2

2
  • Slony can replicate only select tables, but I'm not sure how it handles truncates, and it can be a pain to configure.
  • You might also use something like pgpool to send copies of the insert statements to a second database.
  • You might modify the source of your chat application to do two writes (one to each db) when a new record is created.
  • You could just write a script in Perl/PHP/Python to read from one and write to another, then fire it by cron so that you're sure it gets run before truncation.
Sign up to request clarification or add additional context in comments.

Comments

1

If you only copy a batch of rows every other day, you may be better off with a plain INSERT to a different schema in the same database or a different database in the same database cluster (you need something like dblink for that).

The safest / fastest solution in the same database would be a data-modifying CTE. Something along these lines:

WITH del AS (
    DELETE FROM tbl
    WHERE  <some condition>
    RETURNING *
    )
INSERT INTO backup.tbl
SELECT * FROM del;

For true replication consider these official sources:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.