xybrek xybrek - 1 month ago 17x
SQL Question

Export DB with PostgreSQL's PgAdmin-III

How to export a Postgresql db into SQL that can be executed into other


  • Exporting as backup file, doesn't work when there's a difference in version

  • Exporting as SQL file, does not execute when tried to run on a different

I tried exporting a DB with
pgAdmin III
but when I tried to execute the SQL in other
it throws error in the SQL, when I tried to "restore" a Backup file, it says there's a difference in version that it can't do the import/restore.

So is there a "safe" way to export a DB into standard SQL that can be executed plainly in
SQL editor, regardless of which version it is?


Don't try to use PgAdmin-III for this. Use pg_dump and pg_restore directly if possible.

Use the version of pg_dump from the destination server to dump the origin server. So if you're going from (say) 8.4 to 9.2, you'd use 9.2's pg_dump to create a dump. If you create a -Fc custom format dump (recommended) you can use pg_restore to apply it to the new database server. If you made a regular SQL dump you can apply it with psql.

See the manual on upgrading your PostgreSQL cluster.

Now, if you're trying to downgrade, that's a whole separate mess.

You'll have a hard time creating an SQL dump that'll work in any version of PostgreSQL. Say you created a VIEW that uses a WITH query. That won't work when restored to PostgreSQL 8.3 because it didn't support WITH. There are tons of other examples. If you must support old PostgreSQL versions, do your development on the oldest version you still support and then export dumps of it for newer versions to load. You cannot sanely develop on a new version and export for old versions, it won't work well if at all.

More troubling, developing on an old version won't always give you code that works on the new version either. Occasionally new keywords are added where support for new specification features are introduced. Sometimes issues are fixed in ways that affect user code. For example, if you were to develop on the (ancient and unsupported) 8.2, you'd have lots of problems with implicit casts to text on 8.3 and above.

Your best bet is to test on all supported versions. Consider setting up automated testing using something like Jenkins CI. Yes, that's a pain, but it's the price for software that improves over time. If Pg maintained perfect backward and forward compatibility it'd never improve.