0

I have two servers and 5 schemes in Postgres, one server is in production and the other is in the background.

  • Windows server 2019
  • Postgres 12.7

I have these schemas in the postgres database which is by default, I guess it is not the best option. But this is the scenario.

On the production server I have much more data since it is the one that is operating, what I want to do is copy the database on the production server and copy it to the secondary one since I need to do some tests, but safely.

Tried this with no positive result:

pg_dump.exe -U postgres -d postgres --format=c -f db.sql

And on secondary server the following:

pg_restore.exe --create --clean --if-exists -Fc -d postgres -U postgres "db.sql"

I thought I could do it only with the diagrams in the following way:

pg_dump.exe --file "db.sql" --username "postgres" --format=c --blobs --schema-only --schema "schema" "postgres"

pg_restore.exe --username "postgres" --dbname "postgres" --schema-only --verbose --schema "schema" "db.sql"

But I think it would not be a valid option either, would I have to do a clean when restoring? just copy the data with the --only-data option? How could I completely copy the schema at the source, and rewrite it completely at the destination? with the clean option?

Thanks.

2 Answers 2

0

If you are using --clean and --create, pg_restore will run

DROP DATABASE postgres;
CREATE DATABASE postgres;

Then it will try to connect to that database and restore the data. But the DROP DATABASE will fail, because pg_restore is connected to the database you are trying to drop.

If you want to drop and re-create the postgres database, you have to connect to a different database with pg_restore. One possibility is to use template1:

pg_restore --create --clean -d template1 -U postgres db.sql
1
  • Thanks, I'll write it down for next time, in the end I decided to copy the SCHEMA alone Commented Oct 17, 2023 at 16:36
0

I finally found the solution to the problem.

In the end I opted to backup the schemas only, not the database,there were 5 of them, not too many. Use the commands I put above, you only had to drop the schemas in the receiving database as follows:

DROP SCHEMA "name" CASCADE;
CREATE SCHEMA "name";

After this I only had to do the restore as I mentioned above.

Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.