0

This question is similar to Copy table structure into new table

I want to write a php script that that makes a copy of a table including all constraints etc and puts that table onto a different server. What's the best way to do this?

I was thinking something like:

create table moo (like old_moo including defaults including constraints including indexes)

But as far as I know this doesn't copy foreign keys or triggers. Also I dont know if it's possible to get that command to work between different servers (so far I haven't been able to find out how...).

Any clues?

3
  • Why does it need to be PHP? This really looks like a task that's far better suited to the admin tools that come with postgres (pg_dump, pg_dumpall, etc). You could make a PHP script that exec()s the tool, I suppose, but is this really a job you're going to be doing so often that it needs automation? Commented Jan 27, 2012 at 8:35
  • it doesn't necessarily need to be php, it just needs to be scripted. Yeah, it's something that's likely to happen often. I'll read up on the dump stuff now. thanks for the input Commented Jan 27, 2012 at 10:22
  • Your best bet is probably a simple shell script then (BASH or whatever commandline you're using) that dumps remote DB A to a local file with pg_dump, then restores it to remote DB B. Commented Jan 27, 2012 at 12:13

1 Answer 1

1

It cannot be done. How do you want to copy table with constraints like foreign key... when you need another table for this constraint? You cannot have foreign keys between servers.

Sign up to request clarification or add additional context in comments.

1 Comment

if table A refs table B then I would copy table B across then A. Then I want the foreign key in A (on the new server) to reference B (also on the new server)

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.