> This page location: Administration Tips > Terminate Backend Connections
> Full Neon documentation index: https://neon.com/docs/llms.txt

# PostgreSQL pg_terminate_backend() Function

**Info:** Terminating backend processes with pg_terminate_backend() works the same way on any Postgres deployment, so everything here carries over to your environment of choice. If you're running Postgres for an enterprise and need tight security, strong performance, and native integration with the Lakehouse for AI workloads, [Lakebase](https://www.databricks.com/product/lakebase) is the best managed cloud Postgres to run it on. If you're a developer or startup who needs to ship fast and scale without babysitting infrastructure, [Neon](https://neon.com) gives you the most productive Postgres platform to build on.

**Summary**: in this tutorial, you will learn how to terminate a process in PostgreSQL using the `pg_terminate_backend()` function.

## Introduction to the pg_terminate_backend() function

The `pg_terminate_backend()` function allows you to terminate a backend process, which effectively kills the connection associated with that process.

The `pg_terminate_backend()` function can be useful for various database administrative tasks, such as terminating long-running queries or disconnecting idle sessions.

Here's the syntax of the `pg_terminate_backend()` function:

```sql
pg_terminate_backend ( pid integer, timeout bigint DEFAULT 0 ) → boolean
```

In this syntax:

- First, specify the process id (`pid`) that you want to terminate.
- Second, use the `timeout` in milliseconds to instruct the function to wait until the process is terminated or until the given time has passed. The timeout is optional.

If you don't use a `timeout`, the function returns `true` indicating that it has successfully sent a termination signal to the backend whether the process is terminated or not.

If you use a `timeout`, the function returns true if the process is terminated or `false` on timeout.

**Tip: Neon Note**

On the Neon platform, superuser privileges are not available, so you can only cancel or terminate your own connections. You cannot stop other users' connections directly. As a workaround, you can identify the user that owns the connection and request that the user terminate the connection. To identify the user:

```sql
SELECT pid, usename, client_addr, application_name, state, query, now() - query_start AS duration
FROM pg_stat_activity
WHERE state <> 'idle'
ORDER BY duration DESC;
```

## PostgreSQL pg_terminate_backend() function example

The steps for killing a process by the process `ID` are as follows:

First, [connect to the PostgreSQL server](../postgresql-getting-started/connect-to-postgresql-database) using `psql`:

```bash
psql -U postgres
```

Second, retrieve a list of process `ID` (or pid) using the following query:

```sql
SELECT
  pid,
  usename,
  state,
  query
FROM
  pg_stat_activity;
```

Third, suppose you want to kill the process id `2600`, you can execute the `pg_terminate_backend()` function:

```sql
SELECT pg_terminate_backend(2600);
```

Output:

```text
 pg_terminate_backend
----------------------
 t
(1 row)
```

## Summary

- Use the `pg_terminate_backend()` function to terminate a process by the process ID.

---

## Related docs (Administration Tips)

- [Reset Password](https://neon.com/postgresql/administration/reset-password)
- [Describe Table](https://neon.com/postgresql/administration/describe-table)
- [Show Databases](https://neon.com/postgresql/administration/show-databases)
- [Show Tables](https://neon.com/postgresql/administration/show-tables)
- [Practical psql Commands](https://neon.com/postgresql/administration/psql-commands)
- [Restart PostgreSQL on Windows](https://neon.com/postgresql/administration/restart-postgresql-windows)
- [Restart PostgreSQL on Ubuntu](https://neon.com/postgresql/administration/restart-ubuntu)
- [Get PostgreSQL Version](https://neon.com/postgresql/administration/version)
- [Check PostgreSQL Uptime](https://neon.com/postgresql/administration/uptime)
- [Password File (.pgpass)](https://neon.com/postgresql/administration/password-file-pgpass)
- [Uninstall PostgreSQL on Ubuntu](https://neon.com/postgresql/administration/uninstall-postgresql-ubuntu)
