Mysqldump getting error only when trying to output to file - database

I am using CMD to do a mysqldump. When I run the following command it works fine:
mysqldump -hmyhost -uroot -pmypass mydb
The dump works just fine, however, when it is done, it seems that no .sql file is created for the mysql dump and it just outputs the db data to the screen.
So, I was trying to output the contents to a file using the following command:
mysqldump -hmyhost -uroot -pmypass mydb > C:/mypath/tofile/testdb_dump.sql
But get the following error:
mysqldump: Error: 'Table ' information_schema.files' doesn't exist when trying to dump tablespaces
I greatly appreciate any feedback as to how to resolve this error and backup the db to an .sql file.
Many thanks in advance!

Try mysql -u root -pmypass --opt mydb --no-tablespaces > C:\mypath\tofile\testdb_dump.sql

Related

mongorestore -d mydatabase -c mycollections C:\Users\backup.bson.gz doesn't work

this is my first question here because i really don't know what to do.
So i got a Database named mydatabase.
I also got a Collection named mycollection.
And i got a Backup-File named backup.bson.gz.
I want to use a command in MongoDB / Robo 3T that restores the backup file onto the database.
Because of that i tried the mongorestore command but i get the error:
Error: Line 1: Unexpected identifier
I wrote the command as shown in the title.
Thank you in advance!
use flag --drop
mongorestore -d mydatabase -c mycollections C:\Users\backup.bson.gz --drop

Restoring from Postgres sql file

I have a backup from PostgresSQL database created this way:
/usr/bin/pg_dump --no-owner --no-acl > dump.sql
When I try to restore on a different machine:
psql db < dump.sql it trows many errors: invalid command \N
When I try to use pg_restore:
pg_restore dump.sql -d db
Different error: pg_restore: [archiver] input file appears to be a text format dump. Please use psql.
According to documentation this should be a non issue.
Any way to tell psql that \N character is a null value?
Oh hi me, it's me
Make sure:
Database exists
Importing user has permissions
Get the first error before \N ones flood in
psql -d database -f backup.sql -U user should do the trick for importing
Also eat healthy and get good rest. k bye
this may be the case if the columns in the table and the columns in the file not match

Impossible to restore PostgreSQL database from dump

My computer runs Ubuntu 14.04 with postgreSQL 9.3
I would like to restore a large PSQL database called bigdb on my computer from a dump file (dump file size = 2.3GB).
The dump file comes from a different computer, I currently don't have any prior version of the database on my computer.
I created an empty database called testdb in which I'd like to "write" bigdb
I tried the command:
$pg_restore -C -d testdb dump.dmp
It gave me around 300 errors of this type:
pg_restore: [archiver (db)] Error from TOC entry 15475; 0 25342 TABLE DATA tb_italmaj jturf
pg_restore: [archiver (db)] could not execute query: ERROR: relation "tb_italmaj" does not exist
Command was: COPY tb_italmaj (id_mvtmaj, file_name, file_dh, pgdone, pgdonedh, pgfonctrue, pgerrcode, pgerrmess, pgrowdata) FROM stdin;
At the end, testdb is still empty.
It seems like testdb needs to have the same "structure" (tables,...) as bigdb, but I thought the parameter "-C" would create the database.
I also tried to extract the dump in a plain txt file dump.sql and I tried restoring it with the command:
$psql -v ON_ERROR_STOP=1 testdb < dump.sql
This error popped out immediately:
ERROR: relation "id_tab_eed__texte_seq" does not exist
Likewise it seems that the relations from bigdb need to be created before restoring it and I can't do this.
The only command that executed properly was:
$pg_restore -C dump.dmp
But in this case, no database was created on my computer.
I have been struggling with this issue for several hours. Can someone help me ?
Thanks in advance.
PS: This is my first post and I am not a native english speaker, I hope I was clear. If not, feel free to ask for complementary information.

export MySql database with cmd

I'm using MySql 5.1 version and I'm trying to export mydatabase by cmd ,when I wrote this command :
C:\Program Files\MySQL\MySQL Server 5.1\bin\mysqdump -uroot -ppassword mydatabase >mydatabase.sql
I get an Error
Access is denied
I don't Know what the problem
Help Pleas.....
What's your permissions on the directory you're running this command from? It may be trying to write the mydatabase.sql file to a directory you don't have permissions to.
First check if your command line recognizes mysql command. If not go to command and type in
set path=c:\wamp\bin\mysql\mysql5.1.36\bin
Then use this command to export your database
Go to directory where you have mysql. -u - for username -p - to prompt the password
C:\xampp\mysql\bin>mysqldump -u Username -pPassword DatabaseName > DatabaseName.sql
Note:It is better to use full path of the the sql file file.sql
To export database from dump file (in this case called filename.sql) use: mysql -u username -p password database_name > filename.sql
If you are on Windows you will need to open CMD and go to directory where mysql.exe is installed. If you are using WAMP server then this is usually located in: C:\wamp\bin\mysql\mysql5.1.36\bin (*note the version of mysql might be different)
So you will: cd C:\wamp\bin\mysql\mysql5.1.36\bin
and then execute one of the above commands.

Import SQL dump into PostgreSQL database

We are switching hosts and the old one provided a SQL dump of the PostgreSQL database of our site.
Now, I'm trying to set this up on a local WAMP server to test this.
The only problem is that I don't have an idea how to import this database in the PostgreSQL 9 that I have set up.
I tried pgAdmin III but I can't seem to find an 'import' function. So I just opened the SQL editor and pasted the contents of the dump there and executed it, it creates the tables but it keeps giving me errors when it tries to put the data in it.
ERROR: syntax error at or near "t"
LINE 474: t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view...
The lines:
COPY tb_abilities (active, creation, modtime, id, lang, title, description) FROM stdin;
t 2011-05-24 16:45:01.768633 2011-05-24 16:45:01.768633 view nl ...
I've also tried to do this with the command prompt but I can't find the command that I need.
If I do
psql mydatabase < C:/database/db-backup.sql;
I get the error
ERROR: syntax error at or near "psql"
LINE 1: psql mydatabase < C:/database/db-backu...
^
What's the best way to import the database?
psql databasename < data_base_dump
That's the command you are looking for.
Beware: databasename must be created before importing.
Have a look at the PostgreSQL Docs Chapter 23. Backup and Restore.
Here is the command you are looking for.
psql -h hostname -d databasename -U username -f file.sql
I believe that you want to run in psql:
\i C:/database/db-backup.sql
That worked for me:
sudo -u postgres psql db_name < 'file_path'
I'm not sure if this works for the OP's situation, but I found that running the following command in the interactive console was the most flexible solution for me:
\i 'path/to/file.sql'
Just make sure you're already connected to the correct database. This command executes all of the SQL commands in the specified file.
Works pretty well, in command line, all arguments are required, -W is for password
psql -h localhost -U user -W -d database_name -f path/to/file.sql
Just for funsies, if your dump is compressed you can do something like
gunzip -c filename.gz | psql dbname
As Jacob mentioned, the PostgreSQL docs describe all this quite well.
make sure the database you want to import to is created, then you can import the dump with
sudo -u postgres -i psql testdatabase < db-structure.sql
If you want to overwrite the whole database, first drop the database
# be sure you drop the right database !!!
#sudo -u postgres -i psql -c "drop database testdatabase;"
and then recreate it with
sudo -u postgres -i psql -c "create database testdatabase;"
Follow the steps:
Go to the psql shell
\c db_name
\i path_of_dump [eg:-C:/db_name.pgsql]
I tried many different solutions for restoring my postgres backup. I ran into permission denied problems on MacOS, no solutions seemed to work.
Here's how I got it to work:
Postgres comes with Pgadmin4. If you use macOS you can press CMD+SPACE and type pgadmin4 to run it. This will open up a browser tab in chrome.
If you run into errors getting pgadmin4 to work, try killall pgAdmin4 in your terminal, then try again.
Steps to getting pgadmin4 + backup/restore
1. Create the backup
Do this by rightclicking the database -> "backup"
2. Give the file a name.
Like test12345. Click backup. This creates a binary file dump, it's not in a .sql format
3. See where it downloaded
There should be a popup at the bottomright of your screen. Click the "more details" page to see where your backup downloaded to
4. Find the location of downloaded file
In this case, it's /users/vincenttang
5. Restore the backup from pgadmin
Assuming you did steps 1 to 4 correctly, you'll have a restore binary file. There might come a time your coworker wants to use your restore file on their local machine. Have said person go to pgadmin and restore
Do this by rightclicking the database -> "restore"
6. Select file finder
Make sure to select the file location manually, DO NOT drag and drop a file onto the uploader fields in pgadmin. Because you will run into error permissions. Instead, find the file you just created:
7. Find said file
You might have to change the filter at bottomright to "All files". Find the file thereafter, from step 4. Now hit the bottomright "Select" button to confirm
8. Restore said file
You'll see this page again, with the location of the file selected. Go ahead and restore it
9. Success
If all is good, the bottom right should popup an indicator showing a successful restore. You can navigate over to your tables to see if the data has been restored propery on each table.
10. If it wasn't successful:
Should step 9 fail, try deleting your old public schema on your database. Go to "Query Tool"
Execute this code block:
DROP SCHEMA public CASCADE; CREATE SCHEMA public;
Now try steps 5 to 9 again, it should work out
Summary
This is how I had to backup/restore my backup on Postgres, when I had error permission issues and could not log in as a superuser. Or set credentials for read/write using chmod for folders. This workflow works for a binary file dump default of "Custom" from pgadmin. I assume .sql is the same way, but I have not yet tested that
I use:
cat /home/path/to/dump/file | psql -h localhost -U <user_name> -d <db_name>
Hope this will help someone.
If you are using a file with .dump extension use:
pg_restore -h hostname -d dbname -U username filename.dump
I noticed that many examples are overcomplicated for localhost where just postgres user without password exist in many cases:
psql -d db_name -f dump.sql
You can do it in pgadmin3. Drop the schema(s) that your dump contains. Then right-click on the database and choose Restore. Then you can browse for the dump file.
I used this
psql -d dbName -U username -f /home/sample.sql
Postgresql12
from sql file:
pg_restore -d database < file.sql
from custom format file:
pg_restore -Fc database < file.dump
I had more than 100MB data, therefore I could not restore database using Pgadmin4.
I used simply postgres client, and write below command.
postgres#khan:/$ pg_restore -d database_name /home/khan/Downloads/dump.sql
It worked fine and took few seconds.You can see below link for more information.
https://www.postgresql.org/docs/8.1/app-pgrestore.html

Resources