I am using QGIS version 2.18.14.

When I add a vector layer from a file geodatabase, I choose the OpenFileGdb driver and then pick the layers I would like to have on the map.

enter image description here

However, the layers are added with the name following the format <%DB_NAME% %LAYER_NAME%>. This makes it impossible to use the DBManager SQL editor directly for the added layers as I need to rename the layers first. This is because the database table cannot contain a white space. So, I need to rename "NYC streets" into "streets" or "NYC_streets".

Is there any setting in QGIS that can let me disable prefixing the layers I add with the database name from which they come?

enter image description here


If you are comfortable with a bit of pyqgis magic I can recommend you a solution @Joseph showed me once. You can find his answer here.

I've tried with the sample DB from ArcGIS which was linked by @Steven Kay and the result was the names of files without whitespaces:

import os

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    basename = os.path.splitext(os.path.basename(layer.source()))[0]

To extent this, you could add even more security by using something like this:

basename = basename.replace(" ", "_")

before you use the setLayerName function.


as correctly pointed out from OP the above approach just yields the name of the Database itself without the filename. I changed a bit of the code to tackle this. It looks like this now:

import os

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    basename = layer.name()
    layer.setLayerName(basename.replace(" ", "_"))
  • Thanks, it's helpful to be able to rename the layers in batch using Python after they have been added. The basename variable however would return just the database name, so it doesn't make sense to set all the layers' names to be the database names. But I got the idea, thanks anyway. – Alex Tereshenkov Jan 10 '18 at 9:20
  • @AlexTereshenkov ah you are right. I missed it while quickly trying this approach, my bad. I will leave the answer still as a reference if you dont mind – LaughU Jan 10 '18 at 9:29
  • no worries! Always good to know how to use Python for scripting in QGIS, helpful concepts. – Alex Tereshenkov Jan 10 '18 at 9:40
  • 1
    @AlexTereshenkov I've tried a bit around and found a solution which just replaces whitespaces with underscores ( see edit) – LaughU Jan 10 '18 at 9:41

Just tried this on a sample file geodatabase from ESRI - I see the same thing (except in that case the prefix is "RH_SampleData "). I'm using 2.18.13, GDAL 2.1.3

I don't know of any way to set the layer prefix when you load, but there is a workaround.

In Db Manager SQL queries (using Virtual Layers) you can double-quote table / layer / column names to allow for spaces. This is also useful for when field names match SQL reserved words


select * from "RH_SampleData AADT"

The space does stop the auto-complete from working properly, though. So this is probably no faster or easier than renaming your layers :/

The OpenFileGDB driver seems to be read-only, so renaming your layers should be safe as you won't be able to edit them anyway.

  • Thanks, trick with quotes is helpful. Let's see if someone knows a setting to disable prefixing the layer with the database name. What's worse is that in DBManager > Virtual Layers > QGIS layers you cannot rename the layers either (the changes are discarded and the name remains the same). Too bad. – Alex Tereshenkov Jan 10 '18 at 5:59

You can use Command Bar Plugin which enables you to rename all layers at once. Using QGIS Command Bar you can write:

rename-layers NYC./

then press enter, and it will remove DB name from all layers at once. Note the dot (.) after NYC because it uses regex and the dot means remove everything including the space.

In the following example, I have a File GDB "RH_SampleData.gdb" and when loading the data into QGIS RH_SampleData was added before every layer. I used QGIS Command Bar and rename-layers command to remove RH_SampleData as you can see in the following gif:

enter image description here

  • 1
    Thanks for showing yet another way how to automate layer renaming :) Joseph shown that this can be done in Python, so no need for an extra plugin. I am still wondering whether it's possible to disable prefixing the layers names with the database name as I've stated in the question. Thanks anyway! – Alex Tereshenkov Jan 10 '18 at 9:28

This script will work in QGIS 3. It iterates over all layers in the project and replaces chosen text in their name. Simply paste this inside the QGIS Python Console. (I am not sure about adding import OS in the beginning)

layerList = QgsProject.instance().layerTreeRoot().findLayers()
for layer in layerList:
    basename = layer.name()
    layer.setName(basename.replace("db_prefix_to_be_followed_by_a_space ",""))

or, if you wish to do this only over selected layers in the layer panel:

for layer in iface.layerTreeView().selectedLayers():
    basename = layer.name()
    layer.setName(basename.replace("db_prefix_to_be_followed_by_a_space ",""))

(do not forget to put the offending space inside the first quotation marks) (the empty text inside the second quotation marks will effectively erase the text found in the first quotation marks)

This will serve when adding a database (such as gpkg or fgdb) to a project to remove the database name prefixed on the layer name. This will also make it easier to reference those layers in the DB_manager because the spaces in the names are not compatible with the autocomplete. (as of 3.8)

This is my answer that you can find here Rename layers with PyQGIS script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.