The invention relates to a locking for a safety rail of a bed. The locking is configured to lock the safety rail in the upper position by a releasable coupling of the safety rail to one of the end frames of the bed. The locking can particularly comprise a control element for unlocking the safety rail from the end frame, wherein the locking is configured to be so that it can be unlocked by the control element only when the safety rail has been displaced to a release position. The invention also relates to a method for opening a safety rail, comprising of: - displacing the safety rail from a locked position, in which the locking is locked and non-releasable, to a release position in which the locking can be unlocked; - unlocking the locking in the release position.