Why can't I restore into a newly created database? - postgresql

I have created a backup of my database using the following commands:
pg_dump -Fc mydatabase > db.dump
I can restore this backup using the pg_restore -C by first creating the database outlined in the backup file and then connecting to it. However, when I try to restore the backup with pg_restore without the -C into a newly created database nothing happens. I run the following command:
pg_restore -d newdatabase
When I attempt to view all columns of newdatabase I get:
ERROR: relation "newdatabase" does not exist
I am essentially doing what the documentation told me:
Any help as to why this is happening or how to restore into a newly created database using pg_restorewould be great.
EDIT:
I just checked if newdatabase exists and it does:

Related

update local Dockerized Postgress DB from an equivalent DB on Azure

I have an un-updated local Postgres server, running on a docker container, now I want to update all new records from Production DB,
which runs on Azure Postgres DB.
I'm aware of the pg_dump as in this Answer
but, I'm not clear where should I commend it - the Azure DB doesn't know the Local one and vice versa?
There are multiple methods which you can try.
The most common and simple approach is to use pg_dump and pg_restore commands in bash to upgrade the database.
In above mentioned method, you first create a dump from the source server using pg_dump. Then you restore that dump file to the target server using pg_restore.
To back up an existing PostgreSQL database, run the following command:
pg_dump -Fc -v --host=<host> --username=<name> --dbname=<database name> -f <database>.dump
Once the file has been created, download it in local environment.
After you've created the target database, you can use the pg_restore command and the --dbname parameter to restore the data into the target database from the dump file.
pg_restore -v --no-owner --host=<server name> --port=<port> --username=<user-name> --dbname=<target database name> <database>.dump
To find more upgrade methods, you can refer https://learn.microsoft.com/en-us/azure/postgresql/how-to-upgrade-using-dump-and-restore#method-1-using-pg_dump-and-psql.
To get more details on pg_dump and pg_restore methood, please refer Microsoft Official document Migrate your PostgreSQL database by using dump and restore.

How to create a table from backup file in Postgres?

After making some schema change in table message in postgresql 13 database, the table is backed up in pgadmin4 in a file named message.sql. The schema change needs to be populated in another database. What I did is to drop the table message in that database. But I have hard time to re-create table message from the backup file messages.sql (not using CREATE table message ....). I use pg_restore to restore data but was not successful for re-creating table schema. Here is command I tried with no luck:
pg_restore --data-only -h localhost -U postgres -W -d dynamo -t messages /home/download/messages.sql
pg_restore --clean -h localhost -U postgres -W -d dynamo -t messages /home/download/messages.sql
I also tried to create an blank table (no column) called messages in the database and repeated above command, again without luck.
Here is how a new table is added to the current database:
backup the current db on the server with pg_dump
create the new database on dev PC with addition of new table
On dev PC, use pgadmin to restore the backup file in step 1 to new database created in step 2.
backup in pgadmin and create a .backup file for db in step 3.
on server, use pg_restore to overwrite the current db with new database backup file created in step 4:
pg_restore --clean -U postgres --dbname=mydb -W -h localhost --verbose /home/download/mydb.backup
open psql terminal to verify the new table

How to pg_restore one table and its schema from a Postgres dump?

I am having some difficulties with restoring the schema of a table. I dumped my Heroku Postgres db and I used pg_restore to restore one table from it into my local db (it has more than 20 tables). It was successfully restored, but I was having issues when I tried to insert new data into the table.
When I opened up my database using psql, I found out that the restored table is available with all the data, but its schema has zero rows. Is there anyway I could import both the table and its schema from the dump? Thank you very much.
This is how I restored the table into my local db:
pg_restore -U postgres --dbname my_db --table=message latest.dump
Edit:
I tried something like this following the official docs, but it just gets blocked and nothing happened. My db is small, no more than a couple of megabytes and the table's schema I am trying to restore has no more than 100 row.
pg_restore -U postgres --dbname mydb --table=message --schema=message_id_seq latest.dump
As a more general answer (I needed to restore a single table from a huge backup), you may want to take a look at this post: https://thequantitative.medium.com/restoring-individual-tables-from-postgresql-pg-dump-using-pg-restore-options-ef3ce2b41ab6
# run the schema-only restore as root
pg_restore -U postgres --schema-only -d new_db /directory/path/db-dump-name.dump
# Restore per table data using something like
pg_restore -U postgres --data-only -d target-db-name -t table_name /directory/path/dump-name.dump
From the Heroku DevCenter here
Heroku Postgres is integrated directly into the Heroku CLI and offers
many helpful commands that simplify common database tasks
You can check here if your environment is correctly configured.
In this way, you can use the Heroku CLI pg:pull command to pull remote data from a Heroku Postgres database to a local database on your machine.
For example:
$ heroku pg:pull HEROKU_POSTGRESQL_MAGENTA mylocaldb --app sushi

psql:pr_staging.sql:7624: ERROR: relation "res_company" already exists

Backup command: pg_dump -U username backupdbname -f backupfilename.sql
Restore Command: psql -v ON_ERROR_STOP=1 -f backupfilename.sql -d newdbname;
Actually Tried this command. Backup is working. But while restoring it will throw error psql:pr_staging.sql:7624: ERROR: relation "res_company" already exists. Because for restore, we need one newdb. So that i am creating newdb from browser manually. Thats why i facing the error.
I am creating a new db using terminal command. But it not showing in browser localhost:8069/web/database/selector.
How to restore the backup db?
If you create a db by using Odoo's db manager (interface) there already will be basic tables (the module base will be installed automatically).
There are some ways to restore the db. For example (template0 is a default template db from postgres):
createdb -T template0 newdbname
cat backupfilename | psql newdbname
You shouldn't have a Odoo server running while doing this.
You could also use Odoo's database interface to backup and restore/duplicate databases.

What causes errors when restoring postgres database?

I'm trying to backup up an entire postgres database and restore it properly, however I am seeing a list of errors when trying to restore the backup.
I am using pg_dump to create a backup sql file. (I have a .pgpass file for password)
sudo -u postgres pg_dump -d db-w > backup.sql
When I try to restore the database with:
sudo -u postgres psql db < backup.sql
I get a list of errors like:
ERROR: duplicate key value violates unique constraint
ERROR: multiple primary keys for table
ERROR: relation <relation> already exists
ERROR: trigger <trigger> for relation <relation> already exist
I haven't made any changes to the database. I simply performed a backup and restore the backup right after.
What am I doing wrong?
your restoring on an existing database, if you want and sure to replace database with the backup you can use option --clean and --create
-c, --clean
Clean (drop) database objects before recreating them. (This might
generate some harmless error messages, if any objects were not
present in the destination database.)
-C, --create
Create the database before restoring into it. If --clean is also
specified, drop and recreate the target database before connecting
to it.