Is there a way to repopulate with command-line a postgres db that I can't drop? - postgresql

I'm looking to load a database from a backup.gz. The backup is raw sql generated from pg_dump -U postgres app_development -f backup.gz -Z9.
I've tried dropping the db with psql -Upostgres -c "drop database app_development" but I get:
ERROR: database "app_development" is being accessed by other users
DETAIL: There are 3 other sessions using the database.
The same thing happens when I use dropdb.
I don't want to dump to a non-ascii version so I don't think I can use pg_restore.
Also, I'm not sure if it helps, but all this is happening in docker.

Related

PSQL prevent "COMMENT ON" on the DB dump

We are migrating some products, one of the steps is to migrate the product databases.
I have steps to
export the existing DB pg_dump --no-owner --clean --blobs --no-privileges -U dbuser old_dbname -f bkpfile.sql
import the dump to a different DB psql -U dbuser2 new_dbname -f bkpfile.sql
The problem is the old database contains statement COMMENT ON DATABASE old_dbname IS 'Rxxxxx';
The new DB user must not have permissions on the old database and imho it's not good to refer the old database name anyway in the dump.
Is there a way to create a complete DB dump without the COMMENT ON DATABASE statement?
Edit:
PostgreSQL 9.6
Steps to reproduce:
CREATE DATABASE testdb;
COMMENT ON DATABASE testdb IS 'some comment';
CREATE TABLE xx (id int);
and then dump the database, the dump contains reference to the database name COMMENT ON DATABASE testdb IS 'some comment'; which prevents importing the backup to a new database
pg_dump --no-owner --clean --blobs --no-privileges testdb
We could manually remove the comment statement or filter the comment using different tools (grep), but manual intervention or text-based filtering on top of the backup could cause data corruption.
This comment is only dumped in PostgreSQL versions below v11. See this entry in the release notes:
pg_dump and pg_restore, without --create, no longer dump/restore database-level comments and security labels; those are now treated as properties of the database.
9.6 will go out of support soon anyway, so this is a good opportunity to upgrade.

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

Restoring a Postgresql 10 dump to Postgres 9.3 server

We are about to upgrade from pgsql 9.3 to 10.x. Part of the requirement is to be able to switch back to 9.3 in the case of some disaster (some massive but of course, unlikely incompatibility).
I tried pg_restoring a dump taken from one of our dev v. 10.x databases to a pgsql9.3 server, and got a lot of errors.
Is there any known "roll back path" from v 10.x to v 9.3?
Actually you can use Pg_Dump will give you a full sql file including all DDL and DML statements to recreate your database in another place (or restore).
You can do statement in cmd for backup use Pg_Dump
pg_dump -U username -d database > filename.sql
For more documentation and command use you can see here Pg_Dump
And you can restore use Psql command like this
psql -U username -d database -f filename.sql
You can use the pg_dump from pg9.3 to backup the pg10 database. Then use that backup and pg_restore from pg9.3 again to restore.

How to restore a postgres database

I did a pg_dumpall -c -f <BACKUP_FILE>.sql on a system, I'd like to do a restore on another system and have postgres create all roles/databases/tablespaces for me, is it possible?
This is clearly outlined in the PostgreSQL docs.
https://www.postgresql.org/docs/9.5/static/backup-dump.html
24.1.1. Restoring the Dump
Text files created by pg_dump are intended to be read in by the psql program. The general command form to restore a dump is
psql dbname < infile
where infile is the file output by the pg_dump command. The database dbname will not be created by this command, so you must create it yourself from template0 before executing psql (e.g., with createdb -T template0 dbname). psql supports options similar to pg_dump for specifying the database server to connect to and the user name to use. See the psql reference page for more information. Non-text file dumps are restored using the pg_restore utility.
The issue was that the most recent PE uses postgres 9.4, I could without errors restore to a postgres server running postgres 9.5. My bad, should have checked the rpm's on the PE puppetDB server first.

PostgreSQL - checking if restored database has all the data as original

I backup my PostgreSQL database once a week. One time, during the restore, the restored database was 2.9GB in size, while original database size was 3.7GB. The reason for different sizes is well explained here and that's not a problem.
I back up database using:
pg_dump -U postgres database_name -f backup_file.sql.
To restore the database I use:
psql -U postgres -d target_database -f backup_file.sql.
My question is what is the best way to test and ensure that the restored database has all the data from original database?