We develop an infinite horizon model of an economy in which banks finance long term assets by placing non-tradable debt among savers. Banks choose the overall principal, interest rate, and maturity of their debt taking into account two opposite forces: (i) investors' preference for short maturities (which stems from their exposure to preference shocks) and (ii) banks' exposure to systemic liquidity crises (during which debt refinancing becomes specially expensive). Importantly, the terms of access to refinancing during crises depend endogenously on banks' aggregate refinancing needs. Due to pecuniary externalities, the unregulated equilibrium exhibits inefficiently short debt maturities. We analyze the possibility of restoring efficiency or improving welfare by means of limits to debt maturity, Pigovian taxes, and liquidity insurance schemes.