How to dump a postgres DB into a .sql file - postgresql

I have a "cinema" DB in postgres and I want to dump all its tables and data in a cinema.sql file.
This file will contain all the sql code for re-creating the schema, tables and filling them with the data.
I already have a bank.sql file (for the "bank" DB) which I can execute via PSQL console in pg Admin III and import using the command
/i *path to my bank.sql file*
Now, I want to produce a cinema.sql file like bank.sql, but I don't know how to do it.
It's not the backup/restore feature of course, because it produces a .backup file.
I've also tried
pg dump > cinema.dump
In PSQL console but I can't find a .sql file anywhere, so I don't think it is what I'm looking for either.
Couldn't find anything useful for what I need in Postgres documentation unfortunately so I hope you can help me because I'm just a beginner.
Thanks.

As mentioned in the comments and the documentation, you should use pg_dump command line tool.
pg_dump cinema > cinema.sql

I've made it! Don't know if it's the 100% right way to do it, though, but I think it is.
I'll report here just in case someone else might need this in the future and it can be of help.
I selected the db I wanted to dump in .sql file and then right click -> backup.
Here, as format, I chose plain instead of custom, and "mydbdump.sql" as file name.
In Dump Options #1 and Dump Options #2 I checked the checkboxes to include everything I needed (e.g. "include CREATE DATABASE statement").
I compared this newly created .sql dump with the one I already had (using Notepad++) and they look the same (even if, of course, they are from different dbs).

Related

What is the easiest way to generate a script to drop and create all objects in a database?

I'm used to working with SQL Server and the SQL Server Management Studio has the option to automatically generate a script to drop and recreate everything in a database (tables/views/procedures/etc). I find that when developing a new application and writing a bunch of junk in a local database for basic testing it's very helpful to have the options to just nuke the whole thing and recreate it in a clean slate, so I'm looking for a similar functionality within postgres/pgadmin.
PGAdmin has an option to generate a create script for a specific table but right clicking each table would be very tedious and I'm wondering if there's another way to do it.
To recreate a clean schema only database you can use the pg_dump client included with a Postgres server install. The options to use are:
-c
--clean
Output commands to clean (drop) database objects prior to outputting the commands for creating them. (Unless --if-exists is also specified, restore might generate some harmless error messages, if any objects were not present in the destination database.)
This option is ignored when emitting an archive (non-text) output file. For the archive formats, you can specify the option when you call pg_restore.
and:
-s
--schema-only
Dump only the object definitions (schema), not data.
This option is the inverse of --data-only. It is similar to, but for historical reasons not identical to, specifying --section=pre-data --section=post-data.
(Do not confuse this with the --schema option, which uses the word “schema” in a different meaning.)
To exclude table data for only a subset of tables in the database, see --exclude-table-data.
clean in Flyway
The database migration tool Flyway offers a clean command that drops all objects in the configured schemas.
To quote the documentation:
Clean is a great help in development and test. It will effectively give you a fresh start, by wiping your configured schemas completely clean. All objects (tables, views, procedures, …) will be dropped.
Needless to say: do not use against your production DB!

Backup taken from pgadmin is smaller than backup taken from pgdump

Hello experts I am using postgres 9.5 . When I take a backup from pgadmin it has 950 MB size but when i take the same db backup from pgdump.exe command the backup size is with 7.5 GB. I am confused which backup file will be secured for me that I can use to restore? the restoring process is also slow in postgresql. Please help me.
When you backup something in pgadmin it just calls pg_dump with appropriate options, so both your backups are made by the same pg_dump utility.
I guess you're comparing dumps in two different formats.
Default format for pg_dump is plain, which is basically an enormous uncompressed SQL file.
As for pgadmin, it uses custom format by default, which is a highly compressed binary file.
Also note that pgadmin always displays the actual pg_dump command used to create your dump in the log window, along with its full output.
You should be able to call this command in your command prompt to generate an identical backup file.
You can read more about different output formats and other pg_dump options in PostgreSQL docs.

PG_restore. What does it do?

I read these docs:
Description
pg_restore is a utility for restoring a PostgreSQL database from an
archive created by pg_dump in one of the non-plain-text formats. It
will issue the commands necessary to reconstruct the database to the
state it was in at the time it was saved. The archive files also allow
pg_restore to be selective about what is restored, or even to reorder
the items prior to being restored. The archive files are designed to
be portable across architectures.
pg_restore can operate in two modes. If a database name is specified,
pg_restore connects to that database and restores archive contents
directly into the database. Otherwise, a script containing the SQL
commands necessary to rebuild the database is created and written to a
file or standard output. This script output is equivalent to the plain
text output format of pg_dump. Some of the options controlling the
output are therefore analogous to pg_dump options.
Obviously, pg_restore cannot restore information that is not present
in the archive file. For instance, if the archive was made using the
"dump data as INSERT commands" option, pg_restore will not be able to
load the data using COPY statements.
but it's still unclear to me if pg_restore just loads database data or if it also creates the structure of the database too.
It depends on the options you pass, and obviously in the stored information in the dump. If you keep reading through the documentation you will see this option:
--data-only
Restore only the data, not the schema (data definitions). Table data, large objects, and sequence values are restored, if
present in the archive.
This option is similar to, but for historical reasons not identical to, specifying --section=data.
That is obviously allowing you to restore only the schema but no the data.

How do I load a .pgbackup file locally? with pgAdminIII?

I downloaded a .pgbackup file but couldn't find information on how to load it into a local db.
The forum I grabbed it from is not very responsive too.
Thank you in advance!
Use pg_restore as per the docs
You can use PgAdmin-III to restore a backup too, there's a "Restore" option in the menus. You have to select a database to restore into in order for this option to be enabled, or you can select the "postgres" database and check the option to create a new database for the restored DB in the restore options dialog.
It's also possible that you're dealing with an ordinary SQL dump. If so, you can load it with the command-line psql tool. There is no way I know of to restore an SQL dump via PgAdmin-III. Details of restoring backups with psql are discussed in the documentation.
I wrote a bit of a rant about the usability of backup and restore in PgAdmin-III a while ago.

How to create sql script of postgres database schema?

I want sql script of postgres 9 database schema which is not on local server. I tried pg_dump command on sql editor of pgAdmin and its not working there. I m not sure where to run that command. Please assist me with the same....
Thanks..
pg_dump is a command line utility; it isn't SQL, so it won't work in pgAdmin or anywhere else that executes SQL.
pgAdmin however does have a facility to do what you want:
Right-click on the database you want to export
Select Backup from the pop-up menu
Chose "format" Plain
Chose "plain option" Only schema
You can also use pgAdmin tool to generate or take plain text Database Backup.
You can generate plain text backup for data or schema only.
I have shared few screen shots, please visit this blog for more details.