def load_postgis_sql(db_name, verbosity=1): """ This routine loads up the PostGIS SQL files lwpostgis.sql and spatial_ref_sys.sql. """ # Getting the path to the PostGIS SQL try: # POSTGIS_SQL_PATH may be placed in settings to tell GeoDjango where the # PostGIS SQL files are located. This is especially useful on Win32 # platforms since the output of pg_config looks like "C:/PROGRA~1/..". sql_path = settings.POSTGIS_SQL_PATH except AttributeError: status, sql_path = getstatusoutput("pg_config --sharedir") if status: sql_path = "/usr/local/share" # The PostGIS SQL post-creation files. lwpostgis_file = os.path.join(sql_path, "lwpostgis.sql") srefsys_file = os.path.join(sql_path, "spatial_ref_sys.sql") if not os.path.isfile(lwpostgis_file): raise Exception("Could not find PostGIS function definitions in %s" % lwpostgis_file) if not os.path.isfile(srefsys_file): raise Exception("Could not find PostGIS spatial reference system definitions in %s" % srefsys_file) # Getting the psql command-line options, and command format. options = get_cmd_options(db_name) cmd_fmt = 'psql %s-f "%%s"' % options # Now trying to load up the PostGIS functions cmd = cmd_fmt % lwpostgis_file if verbosity >= 1: print cmd status, output = getstatusoutput(cmd) if status: raise Exception("Error in loading PostGIS lwgeometry routines.") # Now trying to load up the Spatial Reference System table cmd = cmd_fmt % srefsys_file if verbosity >= 1: print cmd status, output = getstatusoutput(cmd) if status: raise Exception("Error in loading PostGIS spatial_ref_sys table.") # Setting the permissions because on Windows platforms the owner # of the spatial_ref_sys and geometry_columns tables is always # the postgres user, regardless of how the db is created. if os.name == "nt": set_permissions(db_name)
def load_postgis_sql(db_name, verbosity=1): """ This routine loads up the PostGIS SQL files lwpostgis.sql and spatial_ref_sys.sql. """ # Getting the path to the PostGIS SQL try: # POSTGIS_SQL_PATH may be placed in settings to tell GeoDjango where the # PostGIS SQL files are located. This is especially useful on Win32 # platforms since the output of pg_config looks like "C:/PROGRA~1/..". sql_path = settings.POSTGIS_SQL_PATH except AttributeError: status, sql_path = getstatusoutput('pg_config --sharedir') if status: sql_path = '/usr/local/share' # The PostGIS SQL post-creation files. lwpostgis_file = os.path.join(sql_path, 'lwpostgis.sql') srefsys_file = os.path.join(sql_path, 'spatial_ref_sys.sql') if not os.path.isfile(lwpostgis_file): raise Exception('Could not find PostGIS function definitions in %s' % lwpostgis_file) if not os.path.isfile(srefsys_file): raise Exception( 'Could not find PostGIS spatial reference system definitions in %s' % srefsys_file) # Getting the psql command-line options, and command format. options = get_cmd_options(db_name) cmd_fmt = 'psql %s-f "%%s"' % options # Now trying to load up the PostGIS functions cmd = cmd_fmt % lwpostgis_file if verbosity >= 1: print cmd status, output = getstatusoutput(cmd) if status: raise Exception('Error in loading PostGIS lwgeometry routines.') # Now trying to load up the Spatial Reference System table cmd = cmd_fmt % srefsys_file if verbosity >= 1: print cmd status, output = getstatusoutput(cmd) if status: raise Exception('Error in loading PostGIS spatial_ref_sys table.') # Setting the permissions because on Windows platforms the owner # of the spatial_ref_sys and geometry_columns tables is always # the postgres user, regardless of how the db is created. if os.name == 'nt': set_permissions(db_name)
def _create_with_shell(db_name, verbosity=1, autoclobber=False): """ If no spatial database already exists, then using a cursor will not work. Thus, a `createdb` command will be issued through the shell to bootstrap creation of the spatial database. TODO: Actually allow this method to be used without a spatial database in place first. """ # Getting the command-line options for the shell command options = get_cmd_options(False) if hasattr(settings, 'POSTGIS_TEMPLATE'): options += '-T %s ' % settings.POSTGIS_TEMPlATE create_cmd = 'createdb -O %s %s%s' % (settings.DATABASE_USER, options, db_name) if verbosity >= 1: print create_cmd # Attempting to create the database. status, output = getstatusoutput(create_cmd) if status: if created_regex.match(output): if not autoclobber: confirm = raw_input( "\nIt appears the database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % db_name) if autoclobber or confirm == 'yes': if verbosity >= 1: print 'Destroying old spatial database...' drop_cmd = 'dropdb %s%s' % (options, db_name) status, output = getstatusoutput(drop_cmd) if status != 0: raise Exception('Could not drop database %s: %s' % (db_name, output)) if verbosity >= 1: print 'Creating new spatial database...' status, output = getstatusoutput(create_cmd) if status != 0: raise Exception( 'Could not create database after dropping: %s' % output) else: raise Exception('Spatial Database Creation canceled.') else: raise Exception('Unknown error occurred in creating database: %s' % output)
def _create_with_shell(db_name, verbosity=1, autoclobber=False): """ If no spatial database already exists, then using a cursor will not work. Thus, a `createdb` command will be issued through the shell to bootstrap creation of the spatial database. TODO: Actually allow this method to be used without a spatial database in place first. """ # Getting the command-line options for the shell command options = get_cmd_options(False) if hasattr(settings, "POSTGIS_TEMPLATE"): options += "-T %s " % settings.POSTGIS_TEMPlATE create_cmd = "createdb -O %s %s%s" % (settings.DATABASE_USER, options, db_name) if verbosity >= 1: print create_cmd # Attempting to create the database. status, output = getstatusoutput(create_cmd) if status: if created_regex.match(output): if not autoclobber: confirm = raw_input( "\nIt appears the database, %s, already exists. Type 'yes' to delete it, or 'no' to cancel: " % db_name ) if autoclobber or confirm == "yes": if verbosity >= 1: print "Destroying old spatial database..." drop_cmd = "dropdb %s%s" % (options, db_name) status, output = getstatusoutput(drop_cmd) if status != 0: raise Exception("Could not drop database %s: %s" % (db_name, output)) if verbosity >= 1: print "Creating new spatial database..." status, output = getstatusoutput(create_cmd) if status != 0: raise Exception("Could not create database after dropping: %s" % output) else: raise Exception("Spatial Database Creation canceled.") else: raise Exception("Unknown error occurred in creating database: %s" % output)
def create_lang(db_name, verbosity=1): "Sets up the pl/pgsql language on the given database." # Getting the command-line options for the shell command options = get_cmd_options(db_name) # Constructing the 'createlang' command. createlang_cmd = 'createlang %splpgsql' % options if verbosity >= 1: print createlang_cmd # Must have database super-user privileges to execute createlang -- it must # also be in your path. status, output = getstatusoutput(createlang_cmd) # Checking the status of the command, 0 => execution successful if status: raise Exception("Error executing 'plpgsql' command: %s\n" % output)
def create_lang(db_name, verbosity=1): "Sets up the pl/pgsql language on the given database." # Getting the command-line options for the shell command options = get_cmd_options(db_name) # Constructing the 'createlang' command. createlang_cmd = 'createlang %splpgsql' % options if verbosity >= 1: print createlang_cmd # Must have database super-user privileges to execute createlang -- it must # also be in your path. status, output = getstatusoutput(createlang_cmd) # Checking the status of the command, 0 => execution successful if status: raise Exception("Error executing 'plpgsql' command: %s\n" % output)