Re: Embedding postgresql in my application - Mailing list pgsql-interfaces

From Jon Earle
Subject Re: Embedding postgresql in my application
Date
Msg-id 3834.142.46.208.244.1166621717.squirrel@kronos.honk.org
Whole thread Raw
In response to Embedding postgresql in my application  (Murray Cumming <murrayc@murrayc.com>)
List pgsql-interfaces
Murray Cumming wrote:
> I'd like my application (Glom) to contain its own PostgreSQL instance,
> either by linking directly to a copy of the PostgreSQL code or, maybe
> more ideally, by starting its own instance of PostgreSQL as a separate
> process.
>
> Does anyone know of an existing example of this that I can look at?

I wanted to do just this - extract the postgres client code for embedding in
my own application.  It's actually rather easy to do; only "interesting" part
is configuring the pg_config.h for each system that you will build on.  I
simply ran configure on the ones I deal with to get a system specific version
of the header that I can then rename as needed in the future.  Bit of a
brutish hack there but it was done in the name of expediency (aren't they
all).

Anyway... here's my notes on how to extract your own client code from the
postgres distribution.  At the end of it, you'll have a static lib that you
can compile into your app.

How to Create a Standalone PostgreSQL Client Library
Jonathan Earle, 29Sep06
====================================================

Prologue
--------

Obtain postgres tarball from postgres.org and my modded Makefile and place
both in your home directory (or elsewhere - just adjust path references
accordingly).

Disclaimer
----------

This is constructed only for Unix systems as that is the only need I have
for creating this.  Win32 could probably be supported by not deleting the
win32 files and by re-including portions from the original libpq makefile.
This was also built on a Linux system, but the resulting client tree can
then be packed and moved to AIX, HPUX, etc.

Procedure
---------

Unpack and create a new client dir for the standalone code:
 tar zxf postgresql-8.1.4.tar.gz mkdir pgclient

Copy required source files:
 export pq=~/postgresql-8.1.4/src
 cp -a $pq/interfaces/libpq/* pgclient
 cp $pq/backend/utils/mb/encnames.c \   $pq/backend/libpq/ip.c \   $pq/backend/libpq/md5.c \   $pq/port/noblock.c \
$pq/port/pgstrcasecmp.c\   $pq/port/thread.c \   $pq/backend/utils/mb/wchar.c \   pgclient
 

Copy required header files:
 export pq=~/postgresql-8.1.4/src/include/
 cp $pq/getaddrinfo.h \   $pq/postgres_fe.h \   $pq/postgres_ext.h \   $pq/c.h \   $pq/pg_config.h \
$pq/pg_config_manual.h\   $pq/pg_config_os.h \   $pq/port.h \   pgclient
 
 cd $pq; cp --parents \libpq/pqcomm.h \libpq/crypt.h \libpq/libpq-be.h \libpq/hba.h \libpq/ip.h \libpq/libpq-fs.h
\mb/pg_wchar.h\nodes/pg_list.h \nodes/nodes.h \~/pgclient; cd -
 
 cp ~/postgresql-8.1.4/src/port/pg_config_paths.h pgclient

Replace Makefile with modified version:
 cp -f Makefile pgclient

Clean up and Build:
 cd pgclient rm -f *.mak *.def exports.txt libpqdll.c libpq.rc* pthread-win32* win32* Adjust pg_config.h and
pg_config_paths.has required. make lib
 

Initial Validation:
 # ls -l *a ~/postgresql-8.1.4/src/interfaces/libpq/*a -rw-rw-rw- 1 jon user 131652 Sep 29 10:26 libpq.a -rw-rw-rw- 1
jonuser 131652 Sep 28 15:20
 
~/postgresql-8.1.4/src/interfaces/libpq/libpq.a

Testing and usage is left to the reader.  Enjoy!  :)


Makefile
--------

#-------------------------------------------------------------------------
#
# Makefile for standalone libpq library
#
# Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
# $Id$
#
#-------------------------------------------------------------------------

CC = gcc
CPPFLAGS = -DFRONTEND -D_GNU_SOURCE -I.
CFLAGS = -O2 -Wall -Wmissing-prototypes -Wpointer-arith -Winline
-fno-strict-aliasing
LIBS = -lcrypt -lresolv -lnsl -ldl -lm -lbsd
LIBOBJS =  copydir.o dirmod.o exec.o noblock.o path.o pipe.o pgsleep.o
pgstrcasecmp.o sprompt.o thread.o

AROPT = crs
LINK.static = $(AR) $(AROPT)
RANLIB = ranlib

# library parameters
NAME= pq

OBJS=   fe-auth.o fe-connect.o fe-exec.o fe-misc.o fe-print.o fe-lobj.o \   fe-protocol2.o fe-protocol3.o pqexpbuffer.o
pqsignal.ofe-secure.o \   md5.o ip.o wchar.o encnames.o noblock.o pgstrcasecmp.o thread.o \   $(filter crypt.o
getaddrinfo.oinet_aton.o open.o snprintf.o strerror.o,
 
$(LIBOBJS))

all:

lib: all lib$(NAME).a

lib$(NAME).a: $(OBJS)   $(LINK.static) $@ $^   $(RANLIB) $@

clean distclean: clean-lib   rm -f $(OBJS)

clean-lib:   rm -f lib$(NAME).a




pgsql-interfaces by date:

Previous
From: Murray Cumming
Date:
Subject: Re: Embedding postgresql in my application
Next
From: "Carlo Stonebanks"
Date:
Subject: ODBC: how to change search_path in DSN?