Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
problem with create database if not exist via postgres backup and restore tool

I wrote a bat script for executing the Postgres backup and restore tool.

I have a little issue with the restore flow: Its works fine as long as my database exists. But if it doesn't it will fail. My restore command:

"pg_restore.exe" -d postgres://postgres:1234@127.0.0.1:9195/mydb -w -c -v -F c --if-exists "DatabaseBackup_mydb.tar" 2>> "DatabaseRestore_mydb.log"

So I need to modify that command somehow that will handle also a use case in which the database "mydb" doesn't exist, and create it in such a case.

just adding the -C flag won't work in that case.

Any suggestion?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Should work by using postgres://postgres:1234@127.0.0.1:9195/postgres and adding -C. Obviously test on throw away instance. This will connect to postgres database DROP DATABASE IF EXISTS mydb; , then CREATE DATABASE mydb, connect to mydb and then restore the database objects.

To demonstrate:

\l test_db
                       List of databases
 Name | Owner | Encoding | Collate | Ctype | Access privileges 
------+-------+----------+---------+-------+-------------------
(0 rows)

pg_restore -d postgres -c -C -U postgres test_db.out 
pg_restore: while PROCESSING TOC:
pg_restore: from TOC entry 4734; 1262 1170111 DATABASE test_db postgres
pg_restore: error: could not execute query: ERROR:  database "test_db" does not exist
Command was: DROP DATABASE test_db;
pg_restore: warning: errors ignored on restore: 1

 \l test_db
                               List of databases
  Name   |  Owner   | Encoding |   Collate   |    Ctype    | Access privileges 
---------+----------+----------+-------------+-------------+-------------------
 test_db | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(1 row)

pg_restore -d postgres -c -C -U postgres test_db.out

\l test_db
                               List of databases
  Name   |  Owner   | Encoding |   Collate   |    Ctype    | Access privileges 
---------+----------+----------+-------------+-------------+-------------------
 test_db | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
(1 row)

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda