I am working on PostgreSQL database and we have a test server which needs to have the same data set as the production one. For this, I plan to start a daily CRON job in linux and copy the production database along with its contents like tables, rows, columns, sequences.
I checked how to copy databases from one to another, and I used the pg_dump command as I will write it below, but it only copied the database tables, sequences, but not the contents.
What should I do to copy the contents?
pg_dump -C databaseName | ssh -C username#removeHost.com "psql databaseName"
Edit
So, What I did was I deleted the database which was on test server,
created a new empty database and then used the command above, and it
worked. So I guess I need to delete the database then only it will
overwrite it.
What should I do to circumvent this behaviour and do a force update
of the database, or delete the test server database even if it is use
and create a new empty database.
Have you tried to use pg_restore instead of psql ? pg_restore has special arguments for your case: -c -C.
Details here:http://www.postgresql.org/docs/current/static/app-pgrestore.html
An example of a command to dump/transfer/restore a db:
pg_dump -F c databaseName | ssh -C username#removeHost.com 'pg_restore --clean --create -d postgres'
For this command you need an empty db on target instance to connect to. (postgres in example).
database named with -d is used only to issue the initial DROP DATABASE
and CREATE DATABASE commands. All data is restored into the database
name that appears in the archive.
If you already have a db on target instance:
pg_dump -F c databaseName | ssh -C username#removeHost.com 'pg_restore --clean -d databaseName'
Similar question: Use pg_dump result as input for pg_restore
Related
I'm trying to script the database objects and data of my database to later move it to a server where I don't have backup/restore rights. Instead I'm using the Generate Scripts method and I use mssql-scripter to generate the scripts.
I have a .bat file with the following script code to generate my SQL script file.
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql
--schema-and-data --script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --include-dependencies --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database
The problem is that it's also scripting DROP DATABASE and CREATE DATABASE, which I don't want. I would only like to DROP and CREATE database objects and later populate tables with the scripted data.
Has anyone faced this problem and have you found a solution?
After fiddling around with the options for longer, I managed to find the right parameter and work-around to solve my problem.
The exact code that I ran is:
set
timevar=%date:~4,2%%date:~7,2%%date:~10,4%-%time:~0,2%%time:~3,2%%time:~6,2%
mssql-scripter --server 10.100.8.8 -d Dev_db -f .\%timevar%.sql --schema-and-data
--script-drop-create --target-server-version 2016 --target-server-edition Standard --check-for-existence --constraint-names --collation -U ScriptingUser -P 1234 --exclude-use-database --include-objects "dbo." --display-progress
The key change I added the --include-objects parameter, with a twist. The way I changed by scripts is by adding code snippet:
--include-objects "dbo."
This tells mssql-scripter to only script out objects that contain the "dbo." keyword(substring) in the fully qualified name.
Also I remove this parameter from my initial command:
--include-dependencies
since I script out everything in my database under the dbo schema.
This scripts out:
all of the objects in my database
it includes a IF EXISTS check
it issues a DROP query to drop the existing
it issues a CREATE query to create the new one
it issues multiple INSERT statements to also populate the database with data
I have .backup file and i need to restore it to my postgres database.
But when i choose restore in my database and choose my file, i got all the tables with some empty tables. i don't know if i don't choose the right options but this is what i did :
I choose my backup file;
and then i choose this options from "Option1" and "Options2":
There is a simple way to do this. Type the following command in the command prompt to restore the table:
C:\Program Files\PostgreSQL\10\bin>pg_restore.exe -U user -p port -h host -w -Fc -d database-name < backup-file.backup
I'm attempting to use bcp.exe to insert rows into an Azure database.
The command line I am using is:
bcp <database>.dbo.<table> IN <datafile> -n -S <server> -U <username> -P <password> -k -e <errorfile>
When executed against a local database, all is fine, the data is inserted.
When executed against the Azure database, bcp says all rows were sent to SQL Server, the error file is empty, the Azure resource usage shows a spike, but no data has made it into the database.
I have tried a data file with a single row.
I have tried -c and -w.
When executing a similar insert using SSMS, the data is inserted.
Any ideas?
Are you using the tcp syntax in your server name (i.e. tcp:servername.database.windows.net) and a user name convention of username#servername?
I followed this article with success: http://thecodegarden.net/bulk-insert-azure-sql/
Otherwise what you have looks good.
I am setting up a test site to allow users to make changes and I need to restore the database every hour.
I have found this script for the cron that works fine but just for the first time. This script will not restore an existing database because of duplicate entries, so is not good for my test site.
mysql -u user -ppassword databasename < /path/to/backup.sql
All I want to do is to restore my dump database (and possibly all the files as well) but I can't find how to do it.
This is the error that I receive via email when I run this script:
ERROR 1062 (23000) at line 62: Duplicate entry '1' for key 'PRIMARY'
I guess I should have a way of deleting the database first and immediately after restore the original dump.
There are two options that I can think of here:
You can create another backup of your original database with an option that DELETEs the tables before restoring the database. This effectively adds DELETE lines to the generated .sql file. The command to do this is:
mysqldump --add-drop-table -u [username] -p [password] [database] > backup.sql
Use mysqlimport to import a backup for a database that already exists like so:
mysqlimport -u [username] -p [password] [database] backup.sql
Hope this helps.
I am looking for a command line program that extracts a script containing commands for creations of all database objects (tables, views, procedures, generators, types). It should work with MySQL, Firebird, Oracle and MS SQL Server.
As a part of our automatic build process we would like to put in our SVN repository a script for database creation in an automatic fashion. Firebird comes with a tool that do what we want but we prefer a generic one so we can include as an ANT task.
For SQL Server you can use the Database Publishing Wizard
I can't help you with the other DB's, however. Good Luck.
To Script the Schema and Data
sqlpubwiz script -d YourDB -S YourServer -U YourUsernam -P pwdhere C:\DestFile.sql
and to just script the Schema
sqlpubwiz script -d YourDB -S YourServer -U YourUsernam -P pwdhere C:\DestFile.sql -schema