In this study, a sandwich piezoelectric nano-energy harvester model under compressive axial loading with a core layer fabricated of functionally graded (FG) porous material is presented based on the nonlocal strain gradient theory (NSGT). The von Karman type geometric nonlinearity and the axial loading were considered. The electromechanical governing equations were obtained using Hamilton’s principle. The nonlinear vibration frequencies, root mean square (RMS) voltage output and static buckling were obtained using the Galerkin method. The effects of different types of porous distribution, porosity coefficients, length scale parameters, nonlocal parameters, flexoelectricity, excitation frequencies, lumped mass and axial loads on the natural frequency and voltage output of nanobeams were investigated. Results show that the porous distributions, porosity coefficient of porous materials, the excitation frequencies and the axial load have a large effect on the natural frequency and voltage output of the sandwiched piezoelectric nanobeams. When the NSGT is considered, the critical buckling load depends on the values of the nonlocal parameters and strain gradient constants. In addition, the electromechanical conversion efficiency of the post-buckling process is significantly higher than that of the pre-buckling process. The flexoelectric effect can significantly increase the RMS voltage output of the energy harvester.