Send data via PostgreSQL to your Logstash instance provided by Logit.io

PostgreSQL

Collect and ship PostgreSQL logs to Logstash and Elasticsearch.

Filebeat is a lightweight shipper that enables you to send your PostgreSQL application logs to Logstash and Elasticsearch. Configure Filebeat using the pre-defined examples below to start sending and analysing your PostgreSQL application logs.

Step 1 - Install Filebeat

deb (Debian/Ubuntu/Mint)

sudo apt-get install apt-transport-https
wget -qO - https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo 'deb https://artifacts.elastic.co/packages/oss-6.x/apt stable main' | sudo tee /etc/apt/sources.list.d/beats.list

sudo apt-get update && sudo apt-get install filebeat

rpm (CentOS/RHEL/Fedora)

sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
echo "[elastic-6.x]
name=Elastic repository for 6.x packages
baseurl=https://artifacts.elastic.co/packages/oss-6.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md" | sudo tee /etc/yum.repos.d/elastic-beats.repo

sudo yum install filebeat

macOS

curl -L -O https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-6.8.4-darwin-x86_64.tar.gz
tar xzvf metricbeat-6.8.4-darwin-x86_64.tar.gz

Windows

  • Download the Filebeat Windows zip file from the official downloads page.
  • Extract the contents of the zip file into C:\Program Files.
  • Rename the filebeat-<version>-windows directory to Filebeat.
  • Open a PowerShell prompt as an Administrator (right-click the PowerShell icon and select Run As Administrator). If you are running Windows XP, you may need to download and install PowerShell.
  • Run the following commands to install Filebeat as a Windows service: cd 'C:\Program Files\Filebeat' .\install-service-filebeat.ps1
If script execution is disabled on your system, you need to set the execution policy for the current session to allow the script to run. For example: PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-filebeat.ps1

Step 2 - Locate Configuration File

deb/rpm /etc/filebeat/filebeat.yml
mac/win Open C:\Program Files\Filebeat\filebeat.yml

Step 3 - Enable the PostgreSQL Module

There are several built in filebeat modules you can use. To enable the PostgreSQL module run.

deb/rpm

filebeat modules list
filebeat modules enable postgresql

macOS

./metricbeat modules list
./metricbeat modules enable PostgreSQL

Windows

.\Filebeat modules enable postgresql

Additional module configuration can be done using the per module config files located in the modules.d folder, most commonly this would be to read logs from a non-default location

deb/rpm /etc/filebeat/modules.d/
mac/win <EXTRACTED_ARCHIVE>/modules.d/

Step 4 - Configure Output

We'll be shipping to Logstash so that we have the option to run filters before the data is indexed.
Comment out the elasticsearch output block.

## Comment out elasticsearch output
#output.elasticsearch:
#  hosts: ["localhost:9200"]

Uncomment and change the logstash output to match below.

output.logstash:
    hosts: ["your-logstash-host:your-port"]
    loadbalance: true
    ssl.enabled: true

Step 5 - (Optional) Update Logstash Filters

All Logit stacks come pre-configured with popular Logstash filters. We would recommend that you add PostgreSQL specific filters if you don't already have them, to ensure enhanced dashboards and modules work correctly.

Edit your Logstash filters by choosing Stack > Settings > Logstash Filters

	if [fileset][module] == "postgresql" {
   		grok {
      		match => {
         "message" => "^%{LOCALDATETIME:[postgresql][log][timestamp]} %{WORD:[postgresql][log][timezone]} \[%{NUMBER:[postgresql][log][thread_id]}(-%{BASE16FLOAT:[postgresql][log][core_id]})?\] ((\[%{USERNAME:[postgresql][log][user]}\]@\[%{POSTGRESQL_DB_NAME:[postgresql][log][database]}\]|%{USERNAME:[postgresql][log][user]}@%{POSTGRESQL_DB_NAME:[postgresql][log][database]}) )?%{WORD:[postgresql][log][level]}:  (duration: %{NUMBER:[postgresql][log][duration]} ms  statement: %{GREEDYDATA:[postgresql][log][query]}|%{GREEDYDATA:[postgresql][log][message]})"
      }
      pattern_definitions => {
         "LOCALDATETIME" => "[-0-9]+ %{TIME}"
         "GREEDYDATA" => "(.|
|	)*"
"POSTGRESQL_DB_NAME" => "[a-zA-Z0-9_]+[a-zA-Z0-9_\$]*"
      }
   }
   date {
      match => [
         "[postgresql][log][timestamp]",
         "yyyy-MM-dd HH:mm:ss.SSS",
         "yyyy-MM-dd HH:mm:ss"
      ]
      target => "@timestamp"
      }
   }

Step 6 - Validate Configuration

Let's check the configuration file is syntactically correct.

Run from the extracted archive dir:

filebeat -e -c filebeat.yml

Step 7 - Start filebeat

Ok, time to start ingesting data!

deb/rpm

sudo systemctl enable filebeat
sudo systemctl start filebeat

Windows

Start-Service filebeat
expand view

Expand View

compact view

Compact View

Return to Search
Sign Up