I am trying to move a schema from one database to another using pg_dump.
I am running this command to take a backup of my schema
pg_dump.exe -h myhost -U postgres -d mydb-n schema> C:\Temp\schema.dump
To restore it I copy the SQL into pgadmin, but I get this error. I don't know what the issue is ?
ERROR: syntax error at or near "2"
LINE 284: 2 \N Unregistered. Item \N 2016-07-13 00:00:00 \N
Is there a best recommended way to backup/restore a postgres database? I often run into issues similar issues when backing up/restoring databases and I start to worry that I will loose valuable data one day.
A plain text pg_dump cannot be restored with pgAdmin since pgAdmin cannot perform COPY FROM STDIN, which is used by pg_dump for the data by default.
You'll have to use psql to restore the dump.
An alternative would be to use the --inserts option of pg_dump so that the data are dumped as INSERT statrments. This comes with a performance penalty though.
Related
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
What's the proper way to do it?
Do I just copy the .sq3 file?
What if there are users on the site and file is being written while it's being copied?
The sqlite3 command line tool features the .backup dot command.
You can connect to your database with:
sqlite3 my_database.sq3
and run the backup dot command with:
.backup backup_file.sq3
Instead of the interactive connection to the database, you can also do the backup and close the connection afterwards with
sqlite3 my_database.sq3 ".backup 'backup_file.sq3'"
Either way the result is a copy named backup_file.sq3 of the database my_database.sq3.
It's different from regularly file copying, because it takes care of any users currently working on the database. There are proper locks set on the database, so the backup is done exclusively.
.backup is the best way.
sqlite3 my_database .backup my_database.back
you can also try .dump command , it gives you the ability to dump the entire database or tables into a text file. If TABLE specified, only dump tables matching LIKE pattern TABLE.
sqlite3 my_database .dump > my_database.back
A good way to make an archival copy using dump and store, Reconstruct the database at a later time.
sqlite3 my_database .dump | gzip -c > my_database.dump.gz
zcat my_database.dump.gz | sqlite3 my_database
Also check this question Do the SQLite3 .backup and .dump commands lock the database?
For streaming replication of SQLite, check out Litestream.
Compared to using the sqlite3-backup command, this is automatic, and incremental.
If you need to restore from backup, the data will be a lot more up to date than if you did a regular backup every hour for example.
Short and simple answer would be
sqlite3 m_database.sq3 ".backup m_database.sq3.bak"
I'm trying to drop my database and create a new one through the command line.
I login using psql postgres and then do a \list, see a list of the two databases i created which i now want to delete. so i tried using a DROP DATABASE databasename;
I don't see any error while executing that statement but when i try to \list again to see if that DB are deleted, i still see that that the DB exists. Can someone please tell me why this could happen? and how to surely delete those DB.
There are a couple caveats to DROP DATABASE:
It can only be executed by the database owner.
It cannot be executed while you or anyone else are connected to the target database.
I generally use the dropdb command-line tool to do this, since it's a wrapper around DROP DATABASE which doesn't require you to explicitly connect first. It still has the caveat that there can't be any users currently connected to the database, but it's generally quicker/easier to use.
I would recommend you try issuing a command like this:
dropdb -h <host> -U <user> -p <port> <name of db to drop>
Similarly, you can use the createdb command-line tool to create a database.
More info on DROP DATABASE: http://www.postgresql.org/docs/current/static/sql-dropdatabase.html
Edit:
Also, it is worth looking in the Postgres log (likely in /var/log/postgresql by default) to see if perhaps there is anything in there that wasn't surfaced in the results.
I have a ton of postgresql dump files I need to peruse through for data. Do I have to install Postgresql and "recover" each one of them into new databases one by one? Or I'm hoping there's a postgresql client that can simply open them up and I can peek at the data, maybe even run a simple SQL query?
The dump files are all from a Postgresql v9.1.9 server.
Or maybe there's a tool that can easily make a database "connection" to the dump files?
UPDATE: These are not text files. They are binary. They come from Heroku's backup mechanism, this is what Heroku says about how they create their backups:
PG Backups uses the native pg_dump PostgreSQL tool to create its
backup files, making it trivial to export to other PostgreSQL
installations.
This was what I was looking for:
pg_restore db.bin > db.sql
Thanks #andrewtweber
Try opening the files with text editor - the default dump format is plain text.
If the dump is not plain text - try using pg_restore -l your_db_dump.file command. It will list all objects in the database dump (like tables, indexes ...).
Another possible way (may not work, haven't tried it) is to grep through the output of pg_restore your_db_dump.file command. If I understood correctly the manual - the output of pg_restore is just a sequence of SQL queries, that will rebuild the db.
In newer versions you need to specify the -f flag with a filename or '-' for stdout
pg_restore -f - dump_file.bin
I had this same problem and I ended up doing this:
Install Postgresql and PGAdmin3.
Open PGAdmin3 and create a database.
Right click the db and click restore.
Ignore file type.
Select the database dump file from Heroku.
Click Restore.
pg_restore -f - db.bin > db.sql
Dump files are usually text file, if Not compressed, and you can open them with a text editor. Inside you will find all the queries that allow the reconstruction of the database ...
If you use pgAdmin on Windows, can just backup the file as plain text, there is one option when you do backup instead of pg_dump in command line prompt.
I would like to backup a Firebird database but exclude certain tables from the backup, is this possible?
If not, I'd like to make a copy of the Firebird database while it's running (without doing a backup followed by a restore)
Neither gbak nor nbackup seem to support things like this, and yet we have a piece of software here in the company that can do a selective backup, I just have no idea how it works. I'd like to replicate its behaviour.
You can export tables and queries with fbexport
http://fbexport.sourceforge.net
gbak can only do a full database, but can run while Firebird is online.
The closest you can get to using gbak without backing up is to skip the intermediate backup file by piping gbak -B to gbak -R. That should save some time too because both operations happen at the same time. For example,
gbak -B dbhost:mydb stdout -user SYSDBA -password masterkey | gbak -R stdin /firebird/mydbcopy.fdb -user SYSDBA -password masterkey
Otherwise if you just want to move data you are looking at a 3rd party tool. You could also write a procedure to utilize ON EXTERNAL DATA SOURCE to refresh your selected tables locally. (Firebird 2.5 only)
There is a new parameter named skip_d in gbak for version 3 of Firebird, and you can give regex to this parameter.
You can find detail information in here
For example: gbak -b -skip_d '(table1|table2|table3)' -user ...