Generally yes — magnesium (a mineral) and melatonin (a sleep hormone) are not flagged as interacting in NHS, NCCIH, or NIH sources and are commonly used together, but stay within magnesium's supplemental limit, expect additive drowsiness, and check with your pharmacist, especially if you take other medicines.
The two work differently and no authoritative source flags a direct magnesium–melatonin interaction. The NHS list of melatonin interactions (antidepressants, benzodiazepines, warfarin, blood-pressure drugs, NSAIDs, opiates, antibiotics, etc.) does not include magnesium, and the NIH Office of Dietary Supplements lists magnesium's medication interactions only with bisphosphonates, certain antibiotics (tetracyclines, quinolones), and diuretics/PPIs — not melatonin. The main practical issue is additive drowsiness: melatonin's known side effects include sleepiness and dizziness (NCCIH), and the NHS specifically warns against taking sedating remedies alongside melatonin because they deepen drowsiness. There is also limited long-term safety data for melatonin (NCCIH) and limited interaction testing for supplements in general, so caution rather than a safety guarantee is appropriate.
Keep supplemental magnesium at or below the adult tolerable upper limit of 350 mg/day from supplements and medications (this limit excludes magnesium from food) — too much causes diarrhea, nausea, and abdominal cramping (NIH ODS); forms like magnesium oxide and citrate are most likely to loosen stools. Take melatonin only at bedtime and watch for excessive next-day grogginess, dizziness, or headache, and do not drive until you know how it affects you. Separate magnesium from certain antibiotics (tetracyclines, quinolones) and bisphosphonates by several hours, as magnesium reduces their absorption. Avoid stacking either with alcohol or other sedating remedies. Pregnant or breastfeeding people, older adults, children, and anyone on blood thinners, blood-pressure medicines, antidepressants, sedatives, diuretics, PPIs, or anti-seizure/epilepsy drugs should check with a doctor or pharmacist first, and tell them about all supplements you take.
This is general reference, not medical advice, and not a guarantee of safety. Interactions depend on your doses, health conditions, and other medicines. Always confirm with your pharmacist or doctor before combining products, and follow the dosing on each label.