Porting Postgresql server to Android

3,519 views
Skip to first unread message

Keith Thomson

unread,
Mar 1, 2013, 5:42:25 PM3/1/13
to pgan...@googlegroups.com
I have a rooted Android device (Not a phone, it's meant to be an application server with an android touch panel on the front)

Sqlite is not suitable for our needs because we require the ability to connect into the database remotely and query it and didn't want to deal with a web interface for this purpose. (Client requires jdbc.)

I compiled Postgresql with the arm-none-linux-gnueabi toolchain, but when I try to run any of the postgres executables on my device I get the following error:

root@android:/ # initdb -D /data/pgsql
/system/bin/sh: initdb: No such file or directory

root@android:/ # postgres
/system/bin/sh: postgres: No such file or directory

The files DO exist

root@android:/ # busybox which initdb
/system/bin/initdb

I actually get a different error if I try to run a file that doesn't exist,

root@android:/system/pgsql/bin # ./pgnothere
/system/bin/sh: ./pgnothere: not found

Is it not finding its libraries? Android doesn't seem to have an "ldconfig" anywhere that I can use to add the libraries in. I have copied the libraries over to /system/lib/ however with no change in behaviour.

I have used the same toolchain to successfully compile busybox and it does function correctly on the android device.

Gurjeet Singh

unread,
Mar 1, 2013, 6:07:56 PM3/1/13
to pgan...@googlegroups.com

Do these files have the execute bit x set on them?

--
You received this message because you are subscribed to the Google Groups "PgAndroid" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pgandroid+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Keith Thomson

unread,
Mar 1, 2013, 6:59:55 PM3/1/13
to pgan...@googlegroups.com, gur...@singh.im
Yes, all of the binaries are set to a+x

-rwxr-xr-x system   system    5649553 2013-03-01 22:02 postgres
-rwxr-xr-x root     log        103742 2000-01-01 00:16 initdb

It also does the same thing regardless of if it's owned by system or root.

Josh Berkus

unread,
Mar 1, 2013, 7:05:39 PM3/1/13
to pgan...@googlegroups.com
Keith,

Why aren't you executing them with ./ ?

It's likely that they're just not in your path.


--
Josh Berkus
PostgreSQL Experts Inc.
http://pgexperts.com

Keith Thomson

unread,
Mar 1, 2013, 7:07:26 PM3/1/13
to pgan...@googlegroups.com
I copied a few of them to my path for the tests (/system/bin) It actually gives me the same error if I run them without ./ from my path, or if I go to /system/pgsql/bin/ and use ./

Aleksandr Panzin

unread,
Mar 1, 2013, 7:13:26 PM3/1/13
to pgan...@googlegroups.com
Hmm... Did you compile PostgreSQL using Android's NDK? Because the libraries in Android differ from your standard Linux distribution(even an ARM targeted one)

Keith Thomson

unread,
Mar 1, 2013, 7:18:02 PM3/1/13
to pgan...@googlegroups.com
No, I'm using the same method I used to compile the Android root file system before with the cross compiler. I wasn't sure that NDK would work with full daemon applications running outside of Android itself.

I was hoping that Postgres compiled without readline and without zlib would have little enough dependencies that it would work with just the libc in android.

I'm actually building a minimal ubuntu install to chroot to and try to run postgres that way, but it would be nice to not need the entire ubuntu-minimal just to run postgres.

Josh Berkus

unread,
Mar 1, 2013, 7:20:26 PM3/1/13
to pgan...@googlegroups.com
On 03/01/2013 04:18 PM, Keith Thomson wrote:
> No, I'm using the same method I used to compile the Android root file
> system before with the cross compiler. I wasn't sure that NDK would work
> with full daemon applications running outside of Android itself.
>
> I was hoping that Postgres compiled without readline and without zlib would
> have little enough dependencies that it would work with just the libc in
> android.

It should, but nobody's done this before, so it's gonna take some trial
and error.

Keith Thomson

unread,
Mar 1, 2013, 7:23:07 PM3/1/13
to pgan...@googlegroups.com
That was pretty much what I thought. I only wish that it had given me a bit more hints as to what it's missing though. Other systems will tell you what library it can't find.

Also, I had copied all of the libraries in /system/pgsql/lib to /system/lib and that didn't seem to help either.

I'll continue forward with the chroot option for now.

Keith Thomson

unread,
Mar 1, 2013, 8:27:26 PM3/1/13
to pgan...@googlegroups.com
Big Giant Roadblock for the chroot method...

Postgres doesn't allow itself to be run as root. Applications not running as root in android chroot cannot use the network.

I don't see a way around this other than researching android more to find a way to get permission to host a server.

Keith Thomson

unread,
Mar 1, 2013, 8:46:39 PM3/1/13
to pgan...@googlegroups.com
Roadblock circumvented. In case anyone else needs to run postgres under ubuntu inside a chroot jail on android, here's the post about giving the user access to the network

http://forums.gentoo.org/viewtopic-p-6913506.html?sid=ae608c5a3c315065ffb983ecca1db48c#6913506

For those who don't want to visit the Gentoo forums, Add a group with ID 3003 and add postgres to it.

Alan Chandler

unread,
Apr 6, 2013, 5:22:10 PM4/6/13
to pgan...@googlegroups.com
I've been searching around the net for a solution to a problem I have.  I can't quite understand how you have solved it and I haven't

I have used the "Linux deploy" app to create a debian wheezy chroot on my Nexus 7.  I have apache2 working and ssh server without issue, but pg_createcluster is crashing when I try and make InitDB 

I have added the postgres user to the 3003, but in actual fact pg_createcluster seems to need to run under root (which also has been added to the 3003 group)

This is the fault

root@nexus:/etc# pg_createcluster 9.1 main --start
Creating new cluster (configuration: /etc/postgresql/9.1/main, data: /var/lib/postgresql/9.1/main)...
FATAL:  could not create shared memory segment: Function not implemented
DETAIL:  Failed system call was shmget(key=1, size=1908736, 03600).
child process exited with exit code 1
initdb: removing contents of data directory "/var/lib/postgresql/9.1/main"
Error: initdb failed

How did you avoid this?

Keith Thomson

unread,
Jul 26, 2013, 10:35:33 PM7/26/13
to pgan...@googlegroups.com
Sorry for not replying to this sooner, I just noticed it.

I didn't actually encounter that issue at all on my system. It might be some limitation of the version of android you're running on top of. I'm using 4.0.3 compiled by the developers of my board, so it might have been compiled for shared memory. It could be some options configured in my kernel as well. You'd probably need to consult with someone who knows more about linux shared memory.


--
You received this message because you are subscribed to a topic in the Google Groups "PgAndroid" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pgandroid/STg8xObGFlo/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to pgandroid+...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Keith Thomson
Senior OEM Developer

Flow Data, Inc.

2309 Grand Park Drive

Grand Junction, Colorado 81505

970.523.7744 TEL
970.812.1253 DIRECT
970.523.7774 FAX
970.623.4983 CELL
Kei...@Flow-Data.Com
www.flow-data.com

fabr...@sacco.mobi

unread,
Aug 1, 2013, 1:56:56 PM8/1/13
to pgan...@googlegroups.com
I've ported with success postgresql 8.4 (from source, with make), apache2, php5, gcc, sshd (for sftp) so
i've build a complete server wifi android, powerful, fast, portable on a 50$ android rooted table...
installt "debian kit"  app from google play or other place (es. http://it.appszoom.com/android_applications/tools/debian-kit_cfpiv.html), follow the instruction for a minimal debian system, install your packages with apt-get...
Assign id 3003 at postgres user, for solving socket bugs in postgresql server... and.. enjoy
your debian wifi server ....

PS: Thanks to mark kirkwood for help me solving socket problem (link: http://android-dls.com/wiki/index.php?title=Debian_on_G1)

Josh Berkus

unread,
Aug 1, 2013, 2:39:14 PM8/1/13
to pgan...@googlegroups.com
On 08/01/2013 10:56 AM, fabr...@sacco.mobi wrote:
> I've ported with success postgresql 8.4 (from source, with make), apache2,
> php5, gcc, sshd (for sftp) so
> i've build a complete server wifi android, powerful, fast, portable on a
> 50$ android rooted table...
> installt "debian kit" app from google play or other place (es.
> http://it.appszoom.com/android_applications/tools/debian-kit_cfpiv.html),
> follow the instruction for a minimal debian system, install your packages
> with apt-get...
> Assign id 3003 at postgres user, for solving socket bugs in postgresql
> server... and.. enjoy
> your debian wifi server ....

Cool! Time to try it out on my tablet, and burn up my batteries!

Why 8.4, though?

Fabrizio Sacco

unread,
Aug 7, 2013, 8:46:17 AM8/7/13
to pgan...@googlegroups.com
>Cool! Time to try it out on my tablet, and burn up my batteries!

>Why 8.4, though?

I've used this version for one year and it's (I think) the stable
version for debian....
Also i need free space in my little debian.img file....
Are you ok with debian kit and postgresql???
By and thanks!!!

2013/8/1, Josh Berkus <jo...@agliodbs.com>:
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "PgAndroid" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/pgandroid/STg8xObGFlo/unsubscribe.

Shannon Fleming

unread,
Dec 6, 2013, 4:12:18 PM12/6/13
to pgan...@googlegroups.com
No luck for me with this method.
I get the same error reported by Alan Chandler.
I've tried installing PostgreSQL a dozen ways on a dozen flavors and versions of Linux running on Android.
All of them end with the same error: "FATAL:  could not create shared memory segment: Function not implemented"

There's something about Linux running under chroot on Android on Nexus 7.
I've tried several different builds of Android.
Several different flavors and versions of Linux.
I've tried several version of PostgreSQL.
I've tried installing from binaries, installing from apt-get, installing from source.
They all fail.
Anyone know how to "implement" the shmget() function (it appears that is the function that is not implemented).
Anyone know where it is not implemented?
Does that mean that it is not implemented in my Android build or my linux build or is it a bogus error with a different root cause?


postgres@android-df2f4d23b58890db:~$ /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale C.
The default database encoding has accordingly been set to SQL_ASCII.
The default text search configuration will be set to "english".

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 10
selecting default shared_buffers ... 400kB
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... FATAL:  could not create shared memory segment: Function not implemented
DETAIL:  Failed system call was shmget(key=1, size=1515520, 03600).


child process exited with exit code 1

initdb: removing contents of data directory "/usr/local/pgsql/data"

Keith Thomson

unread,
Dec 6, 2013, 4:17:06 PM12/6/13
to pgan...@googlegroups.com
One thing to remember when porting postgres to any ARM platform is that there was a spinlock bug in older versions of Postgres that caused major issues with ARM processors. You need to be using at least 9.1.2 to get the fix for that issue.

Past that, I'm not sure. shmget seems to be a function related to shared memory.

I haven't run postgres on anything newer than android 4.1, so if it's 4.2 or higher they may have disabled something for security reasons.

Fabrizio Sacco

unread,
Dec 7, 2013, 6:18:27 AM12/7/13
to pgan...@googlegroups.com
I've only ported postgresql 8.4 from source, android 4.1 under debian kit, rooted tablet, and i have no problem with initdb... 


2013/12/6 Keith Thomson <kei...@flow-data.com>
Message has been deleted

Shannon Fleming

unread,
Dec 9, 2013, 8:36:39 AM12/9/13
to pgan...@googlegroups.com
When you say 4.1, which patch level are you talking about?
4.1.?

I am running 4.1.2 because Google says that 4.1.1 is not supported on my Nexus 7 (first gen).



Fabrizio Sacco

unread,
Dec 9, 2013, 11:25:30 AM12/9/13
to pgan...@googlegroups.com

I have a tablet with 4.2.2 ... it works

Laro Dee

unread,
Jun 2, 2014, 7:53:18 PM6/2/14
to pgan...@googlegroups.com
Alan,

I'm also facing the same issue on a 4.1.1 Android with 3.4.0 kernel. used the debkit method, initially with squeeze and then dist-upgraded' to wheezy.

same shmget issue with either 8.4 and 9.1. seems to me a kernel limitation, though this sounds more than strange.... shmget should normally be a very fundamental call.

not to this particular Android as it seems.

any insight is welcome, browsed the net from end to end to figure this one out.

best,
Laro

Alan Chandler

unread,
Jun 3, 2014, 12:40:24 AM6/3/14
to pgan...@googlegroups.com, lar...@gmail.com
I cannot help I am afraid.  I never did get this working.  Shortly afterwards decided that the Nexus 7 was not going to be my portable development environment and haven't touched it since.
--
You received this message because you are subscribed to a topic in the Google Groups "PgAndroid" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pgandroid/STg8xObGFlo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pgandroid+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
Alan Chandler
http://www.chandlerfamily.org.uk

GyuMoon Min

unread,
Nov 18, 2014, 9:06:24 PM11/18/14
to pgan...@googlegroups.com
Hello, I'm finding out how to porting postgresql into android platform.
Could you please let me know how you did port the source code?
please let me know how to make Android.mk file.

2013년 8월 2일 금요일 오전 2시 56분 56초 UTC+9, Fabrizio Sacco 님의 말:

Fabrizio Sacco

unread,
Nov 19, 2014, 8:53:00 AM11/19/14
to pgan...@googlegroups.com

I have installed before debian kit application and after i have compiled with make command under Linux debian on rooted android

GyuMoon Min

unread,
Nov 19, 2014, 8:35:29 PM11/19/14
to pgan...@googlegroups.com
Thank you for the reply!
Could you give me an advice for my situation?
My environment is
   Host OS : Ubuntu 12.04
   Android Source version : ISC 4.0.4
   Target : full-eng emulator

I understood your explanation as
   step 1. download postgresql 9.1
   step 2. connect to a emulator using 'adb shell'
   step 3. push the postgresql 9.1 source code into rooted android ?...
   setp 4. making command under ... sorry I don't understand..because I use Ubuntu

Could you please expain little bit more?

Thank you.
Have a good day!

2014년 11월 19일 수요일 오후 10시 53분 0초 UTC+9, Fabrizio Sacco 님의 말:

Fabrizio Sacco

unread,
Nov 20, 2014, 6:19:42 AM11/20/14
to pgan...@googlegroups.com
I understood your explanation as
   step 1. download postgresql 9.1
   step 2. connect to a emulator using 'adb shell'
   step 3. push the postgresql 9.1 source code into rooted android ?...
   setp 4. making command under ... sorry I don't understand..because I use Ubuntu

No,  none of your step 
I do not use Ubuntu or other version of linux...i use debian...I cannot help you in that way...
I have only installed the package "debian kit" 
on my rooted android: follow the the instructions and download the package at:

 After the debian is installed on your android I have:
2) installed the C compiler (gcc package with apt debian command)
3) download the source code of postgresql (i have downloaded  the 8.4 version for saving space on the image file debian.img) I don't know if the 9.1 will be the same... 
4) compile it.... solved many other problems...(for example the postgres user will be in a particular socket group...but not the root user...) etc. etc. ...I did this work 2 years ago for an experiment ...and now i don't remember all the steps....

if you are really interested do the first step (install debian kit : read with attention all the link: http://sven-ola.dyndns.org/repo/debian-kit-en.html )
 you will arrive to have a debian.img file installed correctly on your android,
all I can do for you is give you my debian.img file (about 700 mb) containing:
A) apache2 and php5
B) gcc compiler
C) postgresql 8.4 installed (not in a usal mode :((  but it run!!!)
D) connection php-postgres under apache2 and pgadmin3 (from remote): all ok
E) some examples (a little sql table installed) to show a remote connection on port 80


Excuse me for my english ... i'm italian ...
Bye!!

Keith Thomson

unread,
Nov 20, 2014, 1:07:48 PM11/20/14
to pgan...@googlegroups.com
If you're using ARM based processors, then it's a bad idea to use postgres 8.4, as we found out the hard way. They had a bug in the locking mechanisms for arm back then. You need 9.1 or newer if you're on ARM. We just installed the latest armhf debian core system in a chrooted directory, and then used apt-get to install postgres. We launch it from android with a service script that mounts the /debian folder, then chroots to it and launches the services we need from init.d. We use busybox for the chrooting.

Keith Thomson | Senior Developer

Flow Data, Inc.

2309 Grand Park Dr. | Grand Junction, CO 81505

O: 970.523.7744 | C: 970.623.4983 | F: 970.523.7774

keitht...@flow-data.com

flow-data.com

Grand Junction, CO  |  Denver, CO  |  Connellsville, PA  |  San Antonio, TX  |  Dickinson, ND

GyuMoon Min

unread,
Nov 20, 2014, 6:02:53 PM11/20/14
to pgan...@googlegroups.com

I really appreciate fot the reply. This email is really good for me.
Have a good day!

2014. 11. 20. 오후 8:19에 "Fabrizio Sacco" <fabr...@sacco.mobi>님이 작성:
Reply all
Reply to author
Forward
0 new messages