Received: from localhost (maia-2.hub.org [200.46.204.187]) by postgresql.org (Postfix) with ESMTP id 5C55E9FA39B for ; Wed, 20 Dec 2006 09:49:08 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.187]) (amavisd-new, port 10024) with ESMTP id 16794-05-2 for ; Wed, 20 Dec 2006 09:48:44 -0400 (AST) X-Greylist: delayed 00:20:43.41584 by SQLgrey-1.7.4 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id C75889FA381 for ; Wed, 20 Dec 2006 09:48:49 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from buck.northernsportsman.ca (kronos.honk.org [216.191.241.133]) by svr4.postgresql.org (Postfix) with ESMTP id 4F8E25AF8B6 for ; Wed, 20 Dec 2006 13:28:05 +0000 (GMT) Received: from kronos.honk.org (localhost [127.0.0.1]) by buck.northernsportsman.ca (8.13.4/8.13.4/Debian-3sarge3) with ESMTP id kBKDZGEc024583; Wed, 20 Dec 2006 08:35:17 -0500 Received: from 142.46.208.244 (SquirrelMail authenticated user earlej) by kronos.honk.org with HTTP; Wed, 20 Dec 2006 08:35:17 -0500 (EST) Message-ID: <3834.142.46.208.244.1166621717.squirrel@kronos.honk.org> In-Reply-To: <1166225144.12609.2.camel@localhost.localdomain> References: <1166225144.12609.2.camel@localhost.localdomain> Date: Wed, 20 Dec 2006 08:35:17 -0500 (EST) Subject: Re: Embedding postgresql in my application From: "Jon Earle" To: "Murray Cumming" Cc: pgsql-interfaces@postgresql.org User-Agent: SquirrelMail/1.4.8 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50, FORGED_RCVD_HELO X-Spam-Level: X-Archive-Number: 200612/20 X-Sequence-Number: 6354 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