pg_restore failing to load dump - postgresql

Help! All of a sudden, I'm getting this error when trying to restore my local DB from a dump:
$ pg_restore --no-acl --no-owner --dbname my_db tmp/latest.dump
pg_restore: [archiver (db)] Error while INITIALIZING:
pg_restore: [archiver (db)] could not execute query: ERROR: unrecognized configuration parameter "idle_in_transaction_session_timeout"
Command was: SET idle_in_transaction_session_timeout = 0;
Locally, I'm running postgresql 9.6.3. The dump comes from a server that is running 9.4. No known configuration changes to my machine since this command stopped working.
I understand that idle_in_transaction_session_timeout was introduced in 9.6, but I should still be able to load a dump generated by 9.4. So what gives?

The problem is that you used pg_dump from PostgreSQL 9.6 to create the dump of the 9.4 database.
If you want to create a dump that you can load into a 9.4 database, use pg_dump from PostgreSQL 9.4.

Related

pg_restore for 9.4 fails with error 'could not execute query'

The PostgreSQL version I am trying to restore is 9.4.10. I backed up a database from the same version. The command I am using for restore is:
/opt/PostgreSQL/9.4/bin/pg_restore -U postgres --port=5432 -v --dbname=mydb < /backups/309646/WAL/pipe_309646
The error I get is:
pg_restore: executing BLOB 71197
pg_restore: [archiver (db)] Error from TOC entry 3822; 2613 71197 BLOB 71197 user
pg_restore: [archiver (db)] could not execute query: ERROR: duplicate key value violates unique constraint "pg_largeobject_metadata_oid_index"
DETAIL: Key (oid)=(71197) already exists.
Command was: SELECT pg_catalog.lo_create('71197');
This is repeated in the pg errors some 112 times. I need to restore to the same database. Here is the command I used for dumping:
/opt/PostgreSQL/9.4/bin/pg_dump" -U postgres -Fc -b --port=5432 'mydb' -t public.users > /backups/309646/WAL/pipe_309646
Any indication as to why this is happening? How can I mitigate this error?
You are trying to restore a large object into a database that already contains a large object with the same oid.
Use a new database that does not contain any large objects yet as target for the restore.
Alternatively, drop the large objects first with
SELECT lo_unlink(oid) FROM pg_largeobject_metadata;

How migrate PostgreSQL database from virtual machine to production server?

i tried to migrate database from virtual machine to production server (both working on Ubuntu)
but i faced a lot of problems first when i tired to create backup file with that command
pg_dump mydb --file=db.dump --host=localhost --username=admin
this errors show up
pg_dump: [archiver (db)] query failed: ERROR: permission denied for schema topology
pg_dump: [archiver (db)] query was: LOCK TABLE topology.topology IN ACCESS SHARE MODE
then i tried this command and it goes well
pg_dump -Fc mydb > db.dump
and when i tried to restore db to production server i used this command(after creating an user and a database with that user)
psql -d mydb --file=db.dump
this error show up
The input is a PostgreSQL custom-format dump.
Use the pg_restore command-line client to restore this dump to a database.
Then I use this command to restore it
pg_restore -d mydb db.dump
and it go well but when I run the server using this command
python manage.py runserver
This error shows up
return value.replace(b'\0', b'').decode()
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1-2: invalid continuation byte
Try this as a superuser. Apparently "admin" is not a superuser.
Your first attempt is creating a plain-text backup, which you can restore using "psql". Your second attempt ("-Fc") creates a custom format backup, and you need "pg_restore" to restore it.
And don't forget to migrate the global objects (roles, tablespaces ect), using "pg_dumpall -g".

How do I dump a Google Cloud SQL for PostgreSQL DB to import back into a regular PostgreSQL DB?

I am trying to export my data from a Google Cloud SQL (PostgreSQL) instance in order to import it into a regular Postgres DB using pg_dump and pg_restore:
pg_dump -h sql_proxy -F t --no-owner --no-acl > backup.tar
pg_restore backup.tar -c
However, when running pg_restore I get these errors:
pg_restore: [archiver (db)] Error while PROCESSING TOC: pg_restore:
[archiver (db)] Error from TOC entry 197; 1259 17010 TABLE xxx
postgres pg_restore: [archiver (db)] could not execute query: ERROR:
role "postgres" does not exist
Command was: ALTER TABLE public.xxx OWNER TO postgres;
pg_restore: [archiver (db)] Error from TOC entry 198; 1259 17017 TABLE
xxy postgres pg_restore: [archiver (db)] could not execute query:
ERROR: role "postgres" does not exist
Command was: ALTER TABLE public.xxy OWNER TO postgres;
...
I tried a few variations of flags with no luck. I found many articles on how to migrate the other way around (from PostgreSQL to Google Cloud SQL for PostgreSQL) and the Google Cloud docs only describe how to export data to be imported into a Cloud SQL DB again.
I would appreciate any help on how to avoid the errors above and how to migrate the DB with as little changes as possible.
You need to have the roles that are referenced already pre-created in the instance where you want to import the dump.
There are two ways to achieve that:
use pg_dumpall instead of pg_dump or
pg_dumpall --globals-only and then restore that dump (this will create the roles among other things)

how can I import a database by running just one command?

I try to learn how to import a database into PostgreSQL.
My attempt failed, where I use pg_restore -C ... -d ... to combine creation of a new database and importing from the file to the new database in one command:
$ sudo -u postgres pg_restore -C -d postgres dvdrental.tar
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 2194; 1262 17355 DATABASE pagila postgres
pg_restore: [archiver (db)] could not execute query: ERROR: invalid locale name: "English_United States.1252"
Command was: CREATE DATABASE pagila WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'English_United States.1252' LC_CTYPE = 'English_United States.1252';
pg_restore: [archiver (db)] could not execute query: ERROR: database "pagila" does not exist
Command was: ALTER DATABASE pagila OWNER TO postgres;
pg_restore: [archiver (db)] could not reconnect to database: FATAL: database "pagila" does not exist
My questions are:
I was wondering why my first attempt failed?
Is there a way by running just one command?
The document says:
-C
--create
Create the database before restoring into it. If --clean is also specified, drop and recreate the target database before
connecting to it.
and even provide an example:
Assume we have dumped a database called mydb into a custom-format
dump file:
$ pg_dump -Fc mydb > db.dump
To drop the database and recreate it from the dump:
$ dropdb mydb
$ pg_restore -C -d postgres db.dump
The database named in the -d switch can be any database existing in
the cluster; pg_restore only uses it to issue the CREATE DATABASE
command for mydb . With -C , data is always restored into the
database name that appears in the dump file.
Thanks.
Update:
Thanks again. I found the binary file toc.dat in the dump contains the
CREATE DATABASE pagila WITH TEMPLATE = template0 ENCODING = 'UTF8' LC_COLLATE = 'English_United States.1252' LC_CTYPE = 'English_United States.1252';`
Is it possible modify it to make sudo -u postgres pg_restore -C -d postgres dvdrental.tar work?
Note that I have a working soluton:
Separate creation of a new database in psql and importing from the file to the new database:
template1=# CREATE DATABASE dvdrental;
CREATE DATABASE
$ sudo -u postgres pg_restore -d dvdrental dvdrental.tar
$
Your import failed because the operating system where you are trying to import the dump does not know the locale English_United States.1252. Either the operating system is not Windows, or the Windows doesn't have that locale installed.
pg_restore -C tries to create the database in the same way as it was on the original system, which is not possible. Explicitly creating the database works because it uses a locale that exists.
There is no way to make that CREATE DATABASE command succeed except to have the locale present, so if that is not possible, you have to run two commands to restore the dump.

PostgreSQL backup and restore

I'm having trouble restoring a PostgreSQL backup.
On my own computer I dump a table using the custom backup format.
On the server, I try to restore the file but it gives me several errors:
pg_restore: [archiver (db)] Error while PROCESSING TOC:
pg_restore: [archiver (db)] Error from TOC entry 247; 1259 147321 TABLE county postgres
pg_restore: [archiver (db)] could not execute query: ERROR: type "geometry" does not exist
This is what I've done on the server:
pSql -U postgres
/c justice
CREATE EXTENSION postgis;
pg_restore -p 5432 -U postgres -d justice data_import
I'm running PostgreSQL 9.3.2 on both computers. However I've got PostGIS 2.1.1 on my local computer and 2.0.4 on the server. Could that be a problem?
Why is my PostGIS extension not working on the server?
When I do not specify the database it seems to work. However where does the data go? It also writes out all the SQL statements to the screen.
pg_restore -p 5432 -U postgres public_county
PostgreSQL database dump complete
When I try to verify the installation of PostGIS it gives me an error. I think the extension might be improperly installed:
select PostGIS_full_version();
ERROR: function postgis_full_version() does not exist