简体   繁体   中英

Connecting to postgres database from a phonegap app?

I'm trying to build a phonegap app for ios and android. It's been going well so far but now I hit a major obstacle and I need some help.

I need to connect to a remote Postgres database. I haven't done anything like that before.

Does anyone have any experience/tips for this, or know of any resources which contain relevant information?

From client-side javascript, you can't. Unless phonegap has done something very odd with permissions or provided a PostgreSQL interface (which presumably you'd know about if they had).

What you'll want to do is provide a small server-side wrapper to PostgreSQL that will take requests, convert them to queries and return (presumably) json-formatted results. Oh - and you'll need to think about security too - who can connect, what can they do, does it all need to be encrypted?

If your requirements are simple, this can be easy enough to do in Perl/Python/Ruby etc. or even javascript if you have node.js to hand. With Perl you'd wrap DBIx::Class in a Dancer app - similar modules exist for all the above scripting languages.

Do consider whether you want to run the whole thing over https (let apache handle this for you) - it will avoid issues with passwords/private data being sniffed over wireless connections.

For example, your app would issue an ajax request to: http://myserver/projects/123/messages?limit=20&sort=date

That would be translated into a query into the project-messages table for the last 20 messages sorted by date and wrap the results up as an array of JSON objects (presumably).

You would need to create an API for your data. Then access that API using promises from your js app.

To let the security issues where they belong to (in existing experienced and tested parts of server / client software) and to have a minimum effort of development, I suggest to use some existing lightweight middle ware:

It comes with a docker, where any service you require is packed in, thus making it easy to try it out quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM