Start your 14-day free trial today & Get 20% Off All Annual Managed ELK Plans
No Credit Card Required
Try Logit.io FreeAlready have an account? Sign In
Postgresql Logs
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)
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-oss-7.8.1-amd64.deb
sudo dpkg -i filebeat-oss-7.8.1-amd64.deb
rpm (CentOS/RHEL/Fedora)
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-oss-7.8.1-x86_64.rpm
sudo rpm -vi filebeat-oss-7.8.1-x86_64.rpm
macOS
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-oss-7.8.1-darwin-x86_64.tar.gz
tar xzvf filebeat-oss-7.8.1-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 tofilebeat
. - 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
PowerShell.exe -ExecutionPolicy UnRestricted -File .\install-service-filebeat.ps1
.
Step 2 - 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
./filebeat modules list
./filebeat 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 3 - Locate Configuration File
deb/rpm /etc/filebeat/filebeat.yml
mac/win Open C:\Program Files\Filebeat\filebeat.yml
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-ssl-port"]
loadbalance: true
ssl.enabled: true
Step 5 - Validate configuration
Let's check the configuration file is syntactically correct by running filebeat directly inside the terminal.
If the file is invalid, filebeat will print an error loading config file
error message with details on how to correct the problem.
deb/rpm
sudo filebeat -e -c /etc/filebeat/filebeat.yml
macOS
cd <EXTRACTED_ARCHIVE>
./filebeat -e -c filebeat.yml
Windows
cd <EXTRACTED_ARCHIVE>
.\filebeat.exe -e -c filebeat.yml
Step 6 - (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 7 - Start filebeat
Ok, time to start ingesting data!
deb/rpm
sudo systemctl enable filebeat
sudo systemctl start filebeat
Windows
Start-Service filebeat
Step 8 - PostgreSQL Logging Overview
PostgreSQL (often shortened to Postgres) is a highly stable open-source relational database that supports both relational & non-relational querying. Postgres can run across the majority of operating systems including Linux, Windows & macOS.
PostgreSQL is used by some of the world’s best known brands including Apple, IMDB, Red Hat & Cisco due to its robust feature set, useful addons & scalability.
Some of the benefits of using this database include their support for the majority of programming languages as well as it’s strengths as a reliable transactional database for companies of all sizes.
PostgreSQL users are encouraged to log as much as possible as with insufficient configuration you could easily lose access to key messages for troubleshooting and error resolution. Below are some of the most important logs you’ll likely need to analyse when running Postgres.
PostgreSQL transaction logs help the user to identify what queries a transaction encountered.
Remote Host IP/Name (w/ port) logs can serve to help security technicians identify suspicious activity that has occurred. If you are looking to pinpoint troublesome sessions affecting your infrastructure you might turn to Process ID logs for further insights.
When it comes to logging in Postgres there are twenty three other parameters which can be isolated for troubleshooting using the various keywords; ERROR, FATAL, WARNING, & PANIC.
With all these logs, directories & parameters it is easy to become overwhelmed at the prospect of having to thoroughly analyse your log data & you may wish to use a log management system to streamline your processes.
Our built in PostgreSQL log file analyser helps DBAs, sysadmins, and developers identify issues, create visualisations & set alerts when preconfigured and custom parameters are met.
If you need any assistance with analysing your PostgreSQL logs we're here to help. Feel free to reach out by contacting the Logit support team via live chat & we'll be happy to help you start analysing your data.