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

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.

Related

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".

export postgres db with pg_dump doesn't work, database "db_name" does not exist

im trying to export PostgreSQL Databases (db name ari_company) using pg_dump command:
C:\Program Files (x86)\PostgreSQL\10\bin>pg_dump --no-owner -U postgres ari_company> dump.sql
and then i got this error:
pg_dump: [archiver (db)] connection to database "ari_company" failed: FATAL: database "ari_company" does not exist
but when i run for default database (postgres) it works and dump.sql is created.
with command psql\l i checked which db exist and there is postgres and my created ari_company. what im doing wrong here ?
problem was that i had two installed server but was connecting to wrong server where there is no named database. databse postgres exits on both server because it's created automatically with name like you set for username

pg_restore failing to load dump

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.

postgres restore reporting spurious primary key issue

I'm trying to restore a Postgres database, with the following command:
pg_restore --verbose -h localhost -p 5436 -d my_database --format=c --clean --no-owner --no-privileges --no-tablespaces --jobs=1 Sunday2.backup
During the restore, I see this error:
pg_restore: [archiver (db)] Error from TOC entry 2540; 0 16531 TABLE DATA foo_searchresult uoj9bg6vn4cqm
pg_restore: [archiver (db)] COPY failed for table "foo_searchresult": ERROR: duplicate key value violates unique constraint "foo_searchresult_pkey"
DETAIL: Key (id)=(63) already exists.
CONTEXT: COPY foo_searchresult, line 1
I went back to the source database and ran this:
select id, count(*)
from foo_searchresult
group by id
having count(*) > 1
and got nothing.
Now if I just try to restore that one table to a brand-new database:
pg_restore --verbose -h localhost -p 5436 -d brand_new_database --format=c --clean --no-owner --no-privileges --no-tablespaces --jobs=1 -t foo_searchresult Sunday2.backup
it comes back clean.
UPDATE: I just tried restoring the ENTIRE backup to a brand-new database, and it seems to have made it past foo_searchresult without issue.
(Incidentally, the source database is 9.4, and the target database is 9.5, but I get the same results using the pg_restore from a 9.4 or 9.5 distribution.)
UPDATE: So it seems that dropping the database, then creating an empty one and re-loading that (rather than using the --clean flag) resolved a whole multitude of issues
Anyway, my question was "Has anyone seen this before, or have any idea how to fix it."

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