# Better Stack PostgreSQL logging

## Start logging in 2 minutes

What you would like to collect from your PostgreSQL?

- [Collect both logs and metrics from your PostgreSQL database](#collect-logs-metrics)
- [Collect only logs from your PostgreSQL database](#collect-only-logs)

## Collect logs & metrics

Collect logs and metrics from your PostgreSQL database using [Vector](https://vector.dev) and Prometheus [postgres exporter](https://github.com/prometheus-community/postgres_exporter/).

### 1. Set up PostgreSQL exporter

Create a dedicated PostgreSQL user for Vector to securely scrape metrics:

```bash
[label Create a new user]
psql -h localhost -U postgres \
  -c "CREATE USER better_stack_metrics WITH PASSWORD 'your_password_here'; \
      GRANT SELECT ON pg_stat_database TO better_stack_metrics;"
```

[info]
#### Fill in your password

Replace `your_password_here` with password of your choice.
[/info] 

Create `/etc/postgres_exporter.conf` config file using PostgreSQL user password you've just set:

```makefile
[label /etc/postgres_exporter.conf]
export DATA_SOURCE_NAME="postgresql://better_stack_metrics:your_password_here@localhost:5432/postgres"
```

### 2. Install PostgreSQL exporter

Install latest postgres exporter:

```bash
[label Install postgres exporter]
curl -sSL https://telemetry.betterstack.com/install/postgres_exporter | sudo -E bash
```

### 3. Run exporter

Start the PostgreSQL exporter:

[code-tabs]
```sh
[label Linux]
systemctl start postgres_exporter
```
```sg
[label macOS]
launchctl start com.prometheus.postgres_exporter
```
[/code-tabs]

### 4. Set up Vector

[vector-select-platform]
[vector-platform linux-apt]
Install and configure [Vector](https://vector.dev/) on Ubuntu or Debian:
[/vector-platform]
[vector-platform linux-yum]
Install and configure [Vector](https://vector.dev/) on any Red Hat-based Linux:
[/vector-platform]
[vector-platform macos]
Install and configure [Vector](https://vector.dev/) on macOS:
[/vector-platform]
[vector-platform setup-script-supported]
```bash
[label Set up Vector]
curl -sSL https://telemetry.betterstack.com/setup-vector/postgresql_with_metrics/$SOURCE_TOKEN \
  -o /tmp/setup-vector.sh && \
  bash /tmp/setup-vector.sh
```

[info]
**Curious about what the script does?**

- Installs Vector using official package.
- Configures Vector for your Better Stack source.
- Enables and restarts Vector.

The script will ask for confirmation before making any changes.
[/info]

[/vector-platform]

[vector-platform windows]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/operating-systems/windows/) on Windows using official installer:

```powershell
[label Install Vector]
powershell Invoke-WebRequest https://packages.timber.io/vector/latest/vector-x64.msi -OutFile vector-latest-x64.msi
msiexec /i vector-latest-x64.msi
```


#### Setup

Run in Powershell as administrator to download Vector config:

```powershell
[label Download Vector config using Powershell]
$config_yaml_path = "C:\Program Files\Vector\config\vector.yaml"
$backup_path = "C:\Program Files\Vector\config\vector-backup.yaml"
Move-Item -Path "$config_yaml_path" -Destination "$backup_path"
Invoke-RestMethod -Uri https://telemetry.betterstack.com/vector-yaml/postgresql_with_metrics/$SOURCE_TOKEN |
  Add-Content $config_yaml_path
```


#### Run Vector

Run Vector with new configuration:

```powershell
[label Start Vector]
& "C:\Program Files\Vector\bin\vector" --config "C:\Program Files\Vector\config\vector.yaml"
```


[/vector-platform]
[vector-platform linux-other]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/manual/vector-installer/) using official installer:

```sh
[label Install Vector]
curl --proto '=https' --tlsv1.2 -sSf https://sh.vector.dev | sh -s -- -y
```

Create a [Vector service](https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/):

```sh
[label Create a Vector service]
mkdir -p /etc/vector
mkdir -p /var/lib/vector/
cat <<-'SERVICE' >> /etc/systemd/system/vector.service
[Unit]
Description=Vector
Documentation=https://vector.dev
After=network-online.target
Requires=network-online.target

[Service]
User=vector
Group=vector
ExecStart=/root/.vector/bin/vector
ExecReload=/bin/kill -HUP $MAINPID
Restart=no
AmbientCapabilities=CAP_NET_BIND_SERVICE
EnvironmentFile=-/etc/default/vector

[Install]
WantedBy=multi-user.target
SERVICE
```


#### Setup

Download Vector configuration for your source:

```bash
[label Download Vector config]
CONFIG_YAML_PATH="/etc/vector/vector.yaml"
BACKUP_SUFFIX=".bak-$(date +%Y%m%d%H%M%S)"
mv "${CONFIG_YAML_PATH}" "${CONFIG_YAML_PATH}${BACKUP_SUFFIX}"
curl -o "$CONFIG_YAML_PATH" \
  https://telemetry.betterstack.com/vector-yaml/postgresql_with_metrics/$SOURCE_TOKEN
```


#### Restart Vector

Restart Vector to reload the configuration:

```sh
[label Restart Vector]
systemctl restart vector
```

[/vector-platform]

 
You should see your logs in [Better Stack → Live tail](https://telemetry.betterstack.com/team/0/tail ";_blank").

## Collect only logs

Collect logs from your PostgreSQL database using [Vector](https://vector.dev).

[vector-select-platform]
[vector-platform linux-apt]
Install and configure [Vector](https://vector.dev/) on Ubuntu or Debian:
[/vector-platform]
[vector-platform linux-yum]
Install and configure [Vector](https://vector.dev/) on any Red Hat-based Linux:
[/vector-platform]
[vector-platform macos]
Install and configure [Vector](https://vector.dev/) on macOS:
[/vector-platform]
[vector-platform setup-script-supported]
```bash
[label Set up Vector]
curl -sSL https://telemetry.betterstack.com/setup-vector/postgresql/$SOURCE_TOKEN \
  -o /tmp/setup-vector.sh && \
  bash /tmp/setup-vector.sh
```

[info]
**Curious about what the script does?**

- Installs Vector using official package.
- Configures Vector for your Better Stack source.
- Enables and restarts Vector.

The script will ask for confirmation before making any changes.
[/info]

[/vector-platform]

[vector-platform windows]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/operating-systems/windows/) on Windows using official installer:

```powershell
[label Install Vector]
powershell Invoke-WebRequest https://packages.timber.io/vector/latest/vector-x64.msi -OutFile vector-latest-x64.msi
msiexec /i vector-latest-x64.msi
```


#### Setup

Run in Powershell as administrator to download Vector config:

```powershell
[label Download Vector config using Powershell]
$config_yaml_path = "C:\Program Files\Vector\config\vector.yaml"
$backup_path = "C:\Program Files\Vector\config\vector-backup.yaml"
Move-Item -Path "$config_yaml_path" -Destination "$backup_path"
Invoke-RestMethod -Uri https://telemetry.betterstack.com/vector-yaml/postgresql/$SOURCE_TOKEN |
  Add-Content $config_yaml_path
```


#### Run Vector

Run Vector with new configuration:

```powershell
[label Start Vector]
& "C:\Program Files\Vector\bin\vector" --config "C:\Program Files\Vector\config\vector.yaml"
```


[/vector-platform]
[vector-platform linux-other]
#### Install

Install [Vector](https://vector.dev/docs/setup/installation/manual/vector-installer/) using official installer:

```sh
[label Install Vector]
curl --proto '=https' --tlsv1.2 -sSf https://sh.vector.dev | sh -s -- -y
```

Create a [Vector service](https://www.shubhamdipt.com/blog/how-to-create-a-systemd-service-in-linux/):

```sh
[label Create a Vector service]
mkdir -p /etc/vector
mkdir -p /var/lib/vector/
cat <<-'SERVICE' >> /etc/systemd/system/vector.service
[Unit]
Description=Vector
Documentation=https://vector.dev
After=network-online.target
Requires=network-online.target

[Service]
User=vector
Group=vector
ExecStart=/root/.vector/bin/vector
ExecReload=/bin/kill -HUP $MAINPID
Restart=no
AmbientCapabilities=CAP_NET_BIND_SERVICE
EnvironmentFile=-/etc/default/vector

[Install]
WantedBy=multi-user.target
SERVICE
```


#### Setup

Download Vector configuration for your source:

```bash
[label Download Vector config]
CONFIG_YAML_PATH="/etc/vector/vector.yaml"
BACKUP_SUFFIX=".bak-$(date +%Y%m%d%H%M%S)"
mv "${CONFIG_YAML_PATH}" "${CONFIG_YAML_PATH}${BACKUP_SUFFIX}"
curl -o "$CONFIG_YAML_PATH" \
  https://telemetry.betterstack.com/vector-yaml/postgresql/$SOURCE_TOKEN
```


#### Restart Vector

Restart Vector to reload the configuration:

```sh
[label Restart Vector]
systemctl restart vector
```

[/vector-platform]


You should see your logs in [Better Stack → Live tail](https://telemetry.betterstack.com/team/0/tail ";_blank").

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏


## Additional information

#### Looking for manual instructions to set up Vector?

Find step-by-step instructions in our [dedicated Vector guide](https://betterstack.com/docs/logs/vector/#set-up-vector-manually).