Problem with restoring MySQL database - mysqldump question - passwords

I made a backup of my_database like this:
mysqldump --add-drop-database --databases
--user=my_username --password=my_password my_database > backup.sql
Then I deleted the database completely, and tried to restore it like this:
mysql --user=my_username --password=my_password my_database < backup.sql
I got the following error:
ERROR 1049 (42000): Unknown database 'my_database'
What am I doing wrong ?
I need to be able to restore the database if it was changed somehow or removed completely.
Bonus question: is that possible to read the password from a file rather than providing it in the command line ?

If I understand correctly, what you are doing, then you do not need a 'database' argument for the 'mysql' invocation, since the dump should contain the create database statements.
As for the bonus: you want a --defaults-extra-file option

The mysqldump command does not include the create database statement in its output. You need to create the my_database database before running the second command.

Related

how to take backup of only roles/users in postgresql?

I am trying to restore the backup of the database but it's giving me the error of roles, so I got to know that first, we have to take backup of roles/users then we take complete backup but I am unaware of command that can be used. can anyone help me with the command?
You can use pg_dumpall for that with the --globals-only option:
pg_dumpall --globals-only --file=all_roles_and_users.sql -p postgres -h ...
The file all_roles_and_users.sql will contain all roles and role memberships currently defined in the instance (aka "cluster") you connect to.
This nee

schema does not exist error when using pg_dump and psql

I am trying to migrate files over from one remote database- Scratxh to another remote database. I am using pg_dump and psql to download an .sql file then using psql to recreate the table in the new database - SourceData. I want to copy the table only. I used -t to indicate this, but I still get these errors :
ERROR schema public does not exist
ERROR permission denied to set session authorization.
These are the commands I used.
pg_dump -t table -d Scratch -U me -h host.com > table.sql
psql -d SourceData -U me -h host.com < table.sql
I know that psql command uses the .sql text file to recreate the table, so I tried editing this file to get rid of any mentions of the schema 'public'.
It didn't help. I got the same error.
Has anyone else encountered this?
It is not clear from your comment when the error happens. I will assume it happens on the second command. In that case, the first error shown might be because the second database is not ready to receive the data, i.e.: the SQL contains INSERT statement to a table that doesn't exist yet in SourceData.
You need to create the table in the new database before being able to import data into it.
If you pg_dump the entire database, you would probably not encounter this exact problem.

different command line used to extract tables from an sql server file into one that is usable by mysql

What is the difference between these two command line used to extract tables from a database into one that can be used by mysql ?
C:> mysql -u user -p PASS database_name < ms.sql
And
mysql> source ms.sql ;
I used to do with the former and the database created contained all information but it didn't work. the second worked fine.
Second in the first case setting default character set is examplified but I found none in the homepage of the mysql an example for the second case. I am thankful for any help available.
Both of the commands can be referred as Batch Commands. I am pointing out the difference between them below.
First Command
mysql -u user -p PASS database_name < ms.sql
The above command is executing two commands at a time. One is to loggin to MySQL and other one is, passing the script file to execute using OS I/O Operator '<'.
After execution of this command it will display the sql result of the script and comes back to command prompt.(comes out of SQL
Prompt)
It is necessary to keep USE DB_name command in the begening of file to execute the script
This way is useful when you want to execute a big script without logging into mysql typically most often used.
Second Command
mysql> source ms.sql;
The above command is generally an SQL Command which will execute the script present in sql file.
It is used if you are already in MYSQL Prompt. After executing the script it will return back to Mysql prompt only
You may also use this command like executing the shell script something like mysql> ./filename
For more information please refer MySql Reference Link: https://dev.mysql.com/doc/refman/5.7/en/mysql-batch-commands.html

OpenERP, data restore problem

I am trying to restore a database backup through the client interface of open ERP. A message appeared "Could not restore DB". I am using Postgresql 8.4.1
Please help!
You can restore your db direct from pgadmin by making blank db and restore your db into.
and other way is by command prompt postgres given below command.
To create --- createdb db_name
To restore ---- psql created db_name < from which db you want to restore
Which version of openerp and postgresql you are using ? even this message appears, please check in postgresql you will find your database restored.
Are you able to create backups on the same server? I've had similar problems on new installations when I haven't created a .pgpass file. The db_user and db_password configuration parameters are used during regular database access, but can't be used for PostgreSQL backup and restore operations. For backup and restore, you need to set up a .pgpass file.

PostgreSQL how to create a copy of a database or schema?

Is there a simple way to create a copy of a database or schema in PostgreSQL 8.1?
I'm testing some software which does a lot of updates to a particular schema within a database, and I'd like to make a copy of it so I can run some comparisons against the original.
If it's on the same server, you just use the CREATE DATABASE command with the TEMPLATE parameter. For example:
CREATE DATABASE newdb WITH TEMPLATE olddb;
pg_dump with the --schema-only option.
If you have to copy the schema from the local database to a remote database, you may use one of the following two options.
Option A
Copy the schema from the local database to a dump file.
pg_dump -U postgres -Cs database > dump_file
Copy the dump file from the local server to the remote server.
scp localuser#localhost:dump_file remoteuser#remotehost:dump_file
Connect to the remote server.
ssh remoteuser#remotehost
Copy the schema from the dump file to the remote database.
psql -U postgres database < dump_file
Option B
Copy the schema directly from the local database to the remote database without using an intermediate file.
pg_dump -h localhost -U postgres -Cs database | psql -h remotehost -U postgres database
This blog post might prove helpful for you if you want to learn more about options for copying the database using pg_dump.
This can be done by running the following command:
CREATE DATABASE [Database to create] WITH TEMPLATE [Database to copy] OWNER [Your username];
Once filled in with your database names and your username, this will create a copy of the specified database. This will work as long as there are no other active connections to the database you wish to copy. If there are other active connections you can temporarily terminate the connections by using this command first:
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = '[Database to copy]'
AND pid <> pg_backend_pid();
A good article that I wrote for Chartio's Data School which goes a bit more in depth on how to do this can be found here:
https://dataschool.com/learn/how-to-create-a-copy-of-a-database-in-postgresql-using-psql