pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jon Earle <jepg@kronos.honk.org>
To: Murray Cumming <murrayc@murrayc.com>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: Embedding postgresql in my application
Date: Wed, 20 Dec 2006 08:35:17 -0500 (EST)
Message-ID: <3834.142.46.208.244.1166621717.squirrel@kronos.honk.org> (raw)
In-Reply-To: <1166225144.12609.2.camel@localhost.localdomain>
References: <1166225144.12609.2.camel@localhost.localdomain>

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.h as 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 jon user 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.o fe-secure.o \
    md5.o ip.o wchar.o encnames.o noblock.o pgstrcasecmp.o thread.o \
    $(filter crypt.o getaddrinfo.o inet_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





view thread (12+ messages)  latest in thread

Message-ID: <3834.142.46.208.244.1166621717.squirrel@kronos.honk.org>
Permalink:  ../3834.142.46.208.244.1166621717.squirrel@kronos.honk.org/
Also on:    postgresql.org/message-id/3834.142.46.208.244.1166621717.squirrel@kronos.honk.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: jepg@kronos.honk.org, murrayc@murrayc.com
  Subject: Re: Embedding postgresql in my application
  In-Reply-To: <3834.142.46.208.244.1166621717.squirrel@kronos.honk.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox