DISCLAIMER: This question is similar to the stack overflow question here, but none of those answers work for my problem, as I will explain later.
I\'m t
WARNING:
All the answers which use pg_dump and any sort of regular expression to replace the name of the source table are really dangerous. What if your data contains the substring that you are trying to replace? You will end up changing your data!
I propose a two-pass solution:
Here's an example written in Ruby:
ruby -pe 'gsub(/(members?)/, "\\1_copy_20130320") unless $_ =~ /^\d+\t.*(?:t|f)$/' < members-production-20130320.sql > copy_members_table-20130320.sql
In the above I am trying to copy "members" table into "members_copy_20130320". My data-specific regexp is /^\d+\t.*(?:t|f)$/
The above type of solution works for me. Caveat emptor...
edit:
OK, here's another way in pseudo-shell syntax for the regexp-averse people:
psql -f mytable_copy_schema.sql mydb
pg_dump -a -t mytable mydb > mytable_data.sql