To restore a database from an archive file created using the pg_dump command, use the pg_restore command. Be sure to test your backup and recovery models with test databases.
When you restore a geodatabase or a database with ST_Geometry installed, you must restore schemas in a specific order: first the public schema, then the sde schema, then the remaining schemas.
For general recovery instructions, such as syntax options, see your PostgreSQL documentation. Also, if you have PostGIS installed and are using Geometry storage, be sure to read the PostGIS documentation about creating backups and restoring databases. This procedure could vary depending on the version of PostGIS you are using.
- Database names must be unique on the PostgreSQL database cluster; therefore, if you are restoring to the same database cluster, you must drop the existing database.
dropdb –U sde mypgdb
- If you are restoring to a new database cluster, log in to psql, re-create the sde login role, and grant it superuser privileges.
CREATE ROLE sde LOGIN ENCRYPTED PASSWORD '0shallpass' SUPERUSER INHERIT;
- If you are restoring to a new database cluster, re-create the login roles for all data owners. You can also re-create editor and read-only login roles at this time, though that is not required to restore the database.
The following example script creates data owner, editor, and reader login roles and groups, and adds the login roles to the appropriate groups.
--Re-create dataowners group and login roles. CREATE ROLE dataowners NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; CREATE ROLE owner1 LOGIN ENCRYPTED PASSWORD 'pw.4.owner1' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT dataowners TO owner1; CREATE ROLE owner2 LOGIN ENCRYPTED PASSWORD 'pw.4.owner2' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT dataowners TO owner2; --Re-create editors group and login roles. CREATE ROLE editors NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; CREATE ROLE editor1 LOGIN ENCRYPTED PASSWORD 'pw.4editor1' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT editors TO owner1; CREATE ROLE owner2 LOGIN ENCRYPTED PASSWORD 'pw.4editor2' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT editors TO editor2; --Re-create readers group and login roles. CREATE ROLE readers NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; CREATE ROLE reader1 LOGIN ENCRYPTED PASSWORD 'pw.4reader1' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT readers TO reader1; CREATE ROLE owner2 LOGIN ENCRYPTED PASSWORD 'pw.4reader2' NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION; GRANT readers TO reader2;
- If you are restoring to a new database cluster, be sure to place the st_geometry library in the PostgreSQL lib location. See either Set up a geodatabase in PostgreSQL on Linux or Set up a geodatabase in PostgreSQL on Windows for details on placing the st_geometry library.
- Re-create the database. You can use the createdb statement at the command line or a psql statement to do this.
The following example uses the createdb command line tool to create a database named mypgdb with encoding UTF8, owned by the sde login role, and located in tablespace tblspgdb:
createdb –U sde –E UTF8 –D tblspgdb -O sde mypgdb
- Log back in to psql and create an sde schema owned by the sde login role in the new database. Grant usage on the sde schema to all login roles or groups that will access the geodatabase.
CREATE SCHEMA sde AUTHORIZATION sde; GRANT USAGE ON SCHEMA sde TO dataowners; GRANT USAGE ON SCHEMA sde TO editors; GRANT USAGE ON SCHEMA sde TO readers;
- Alter the search path for the new database to include the sde schema.
ALTER DATABASE mypgdb SET SEARCH_PATH="$user",sde,public;
- At the command line, restore the public schema and data using the pg_restore command. Run the command as a login with superuser privileges, such as the postgres or sde user.
For example, to restore the public schema of a database backup file named mypgdb1031.dump to database mypgdb, run the following:
pg_restore -U sde -d mypgdb -n public mypgdb1031.dump
- Next, restore the contents of the sde schema.
pg_restore -U sde -d mypgdb -n sde mypgdb1031.dump
- Finally, restore the remaining schemas and data.
pg_restore –U sde –d mypgdb mypgdb1031.dump
- Once the database restoration is complete, test the database by connecting to it from ArcGIS for Desktop and examining the data.