farfromfearless
Printing to a matrix printer from PostgreSQL
Recently I found an interesting piece of code again which I would like to share with you.
I produced the code about a year ago for a project which needed offline data logging.
I used the piece of code to print a line to a matrix printer from PostgreSQL. The matrix printer was connected to the database server and supplied with endless paper. I used a matrix printer, because you can print directly to the device.
With the method, described below, it’s possible to print with a SQL query or print something initiated by a trigger. Using this method you can log specific data ‘offline’.
The print_line method consists of three files; print_line.c which contains the code to address the printer, a Makefile to compile the print_line.c file and a print_line.sql file which creates the function in SQL.
Makefile
MODULES = printhash
PGXS := $(shell pg_config --pgxs)
include $(PGXS)
print_line.sql
CREATE FUNCTION print_line(text) RETURNS integer
AS '/usr/local/libexec/print_line.so', 'print_line'
LANGUAGE C STRICT;
Installation:
mkdir /home/user/print_line
cd /home/user/print_line
unzip print_line.zip
make
cp print_line.so /usr/local/libexec/
// open postgresql command line client and execute:
# \i /home/user/print_line/print_line.sql
Usage:
SELECT print_line('Hello World!');
Download:
Download print_line.zip
- Copyright 2012 Melvin’s tech blog. All Rights Reserved.
- Back To Top
- Home

Leave a Comment-