I've been using pgAdmin
to create a database and test it.
I'm at a point now where I need to install this db on different servers. I've been using pg_dump
to do this and I've been able to muddle my way through.
But I'm wondering if there's a way to "reset" all the "current" values in any sequence that I have set up?
If you have any other general suggestions on how to create "empty" or "blank" databases, can you let me know?
Best Answer
I would use
pg_dump
with the--schema-only
option to create the dump for an empty database:Using Linux, you could also pipe the output to
psql
and create a new, empty database in the same (or any other) database cluster right away. Example for the same cluster:If you should create the database in a different db cluster, remember that roles or tablespaces are cluster-wide objects and not included in the dump of a single db. So, any custom roles (or other cluster-wide objects) have to be present in the new cluster before restoring to it.
This is much faster than truncating existing tables.
There is a dedicated command for that:
Unless defined otherwise, this resets a sequence to
1
. To define otherwise, use:Or save the desired start value with the sequence:
And use the simple form to
RESTART
.To restart all sequences in the database, retrieve them from the catalog table
pg_class
:Sufficient privileges required. Superusers pass this test automatically.
Careful with that! You normally don't want to lose the current value for all sequences in your database. In any case, you may want to check what the
SELECT
cooks up before executing it.So, I would create an empty copy and then run the above
DO
command. Thenpg_dump
again and restore to different servers.