In terms of longitudinal stability, their relative position is very important. Particularly, the position of the aerodynamic center with respect to the center of gravity determines whether the aircraft is stable and "how" stable it is.
In order to be stable, the aerodynamic center has to be behind the center of gravity. If you imagine a sudden increase in the angle of attack (e.g. gust) so that the nose goes up, the resulting increase in lift would bring the nose back down. The further back the aerodynamic center, the more stable the aircraft.
You should notice that in general stability and manoeuvrability goes in opposite directions: a very stable aircraft will have a low manoeuvrability.
As Giuseppe Pennisi wrote, the distance between the center of gravity and the aerodynamic center determines the stability of an aircraft. In order to change the distance and thus the stability characteristics, it is difficult to shift the aerodynamic centre. The most convenient way to achieve this is to shift the center of gravity.
As stated earlier, the Aerodynamic stability of an aircraft is largely static (a known). The moment of inertia of secondary weight (passengers, fuel, cargo) varies from flight to flight (pseudo-unknown).
An airplane's center of gravity is not some optional piece of equipment; it's actually one of the most important components of the airplane when it comes to controlling its trajectory. A plane's center of gravity, determined with precise calculations, is a critical factor in guiding and stabilizing the aircraft for a successful flight.
In a typical light general aviation aircraft, the center of gravity is located just aft, or toward the rear, of the firewall, or just aft of where the engine is. The engine, avionics, pilot, and passengers are the heaviest components on an airplane, and the location of the heaviest items on the airplane will be the approximate location of the center of gravity. On larger aircraft, the CG may vary wildly with fuel locations and loading considerations, making the proper loading of the aircraft imperative to a successful flight.
Although the distance between the center of gravity and the aerodynamic center matters, the contribution of the horizontal tail volume to longitudinal stability should also be mentioned.
If the question is which of the two centers to shift, the answer is the aerodynamic center. From the design point of view there is not much that can be done to shift the center of gravity. On the contrary, the aerodynamic center can be shifted by relocating the wing.