user1702964
user1702964

Reputation: 1119

Exporting sequences in PostgreSQL

I want to export ONLY the sequences created in a Database created in PostgreSQL. There is any option to do that?

Thank you!

Upvotes: 8

Views: 13459

Answers (3)

scrollout
scrollout

Reputation: 577

Using DBeaver, you can

  1. open a schema
  2. select its sequences
  3. crtl-F to search for the sequences you're interested in
  4. crtl-A to select all of them
  5. Right-click and select Generate SQL -> DDL

You will be given SQL statements to create all of the sequences selected.

Upvotes: -1

Mani Dwivedi
Mani Dwivedi

Reputation: 73

I know its too old but today I had similar requirement so I tried to solve it the same way by creating a series of "CREATE SEQUENCE" queries which can be used to RE-create sequences on the other DB with bad import (missing sequences) here is the SQL I used:

SELECT 
'CREATE SEQUENCE '||c.relname||
' START '||(select setval(c.relname::text, nextval(c.relname::text)-1))
AS "CREATE SEQUENCE SQLs"
FROM 
  pg_class c 
WHERE 
  c.relkind = 'S'

Maybe that can be helpful for someone.

Upvotes: 5

Kuberchaun
Kuberchaun

Reputation: 30362

You could write a query to generate a script that will create your existing sequence objects by querying this information schema view.

select *
from information_schema.sequences;

Something like this.

SELECT 'CREATE SEQUENCE ' || sequence_name || ' START ' ||  start_value || ';'
from information_schema.sequences;

Upvotes: 14

Related Questions