Properly select database name if it's defined