|
| 1 | +# PostgreSQL Wire-Compatible Server |
| 2 | + |
| 3 | +QueryLeaf includes a PostgreSQL wire-compatible server that allows you to connect to your MongoDB database using standard PostgreSQL clients like `psql`, pgAdmin, DBeaver, or any application that supports PostgreSQL connectivity. |
| 4 | + |
| 5 | +This provides a convenient way to use SQL with your MongoDB database without requiring specialized drivers or custom integration work. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +The PostgreSQL server is included in the QueryLeaf package and can be installed via npm: |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install -g @queryleaf/postgres-server |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +### Starting the Server |
| 18 | + |
| 19 | +You can start the server using the following command: |
| 20 | + |
| 21 | +```bash |
| 22 | +queryleaf-pg-server --db your_database_name |
| 23 | +``` |
| 24 | + |
| 25 | +By default, the server will connect to MongoDB at `mongodb://localhost:27017` and listen for PostgreSQL connections on port 5432. |
| 26 | + |
| 27 | +### Command Line Options |
| 28 | + |
| 29 | +The server supports the following command line options: |
| 30 | + |
| 31 | +| Option | Description | Default | |
| 32 | +| ------ | ----------- | ------- | |
| 33 | +| `--uri` | MongoDB connection URI | `mongodb://localhost:27017` | |
| 34 | +| `--db` | MongoDB database name (required) | - | |
| 35 | +| `--port` | PostgreSQL server port | `5432` | |
| 36 | +| `--host` | PostgreSQL server host | `localhost` | |
| 37 | +| `--max-connections` | Maximum number of connections | `100` | |
| 38 | + |
| 39 | +Example with custom options: |
| 40 | + |
| 41 | +```bash |
| 42 | +queryleaf-pg-server --db your_database_name --uri mongodb://user: [email protected]:27017 --port 5433 --host 0.0.0.0 |
| 43 | +``` |
| 44 | + |
| 45 | +## Connecting with PostgreSQL Clients |
| 46 | + |
| 47 | +Once the server is running, you can connect to it using any PostgreSQL client. Authentication is currently simplified - any username will be accepted. |
| 48 | + |
| 49 | +### Using psql |
| 50 | + |
| 51 | +```bash |
| 52 | +psql -h localhost -p 5432 -d your_database_name -U any_username |
| 53 | +``` |
| 54 | + |
| 55 | +### Connection String |
| 56 | + |
| 57 | +The PostgreSQL connection string format is: |
| 58 | + |
| 59 | +``` |
| 60 | +postgresql://username@host:port/database |
| 61 | +``` |
| 62 | + |
| 63 | +For example: |
| 64 | + |
| 65 | +``` |
| 66 | +postgresql://user@localhost:5432/your_database_name |
| 67 | +``` |
| 68 | + |
| 69 | +## Supported Features |
| 70 | + |
| 71 | +The PostgreSQL server supports: |
| 72 | + |
| 73 | +- Basic SQL queries (SELECT, INSERT, UPDATE, DELETE) |
| 74 | +- Transaction management (BEGIN, COMMIT, ROLLBACK) |
| 75 | +- Connection pooling for multiple concurrent clients |
| 76 | + |
| 77 | +## Limitations |
| 78 | + |
| 79 | +The current implementation has the following limitations: |
| 80 | + |
| 81 | +- Limited support for PostgreSQL-specific features and data types |
| 82 | +- Authentication is simplified (any username/password is accepted) |
| 83 | +- Some advanced PostgreSQL features may not be supported |
| 84 | +- Performance may differ from native PostgreSQL |
| 85 | + |
| 86 | +## Programmatic Usage |
| 87 | + |
| 88 | +You can also use the PostgreSQL server programmatically in your Node.js applications: |
| 89 | + |
| 90 | +```typescript |
| 91 | +import { MongoClient } from 'mongodb'; |
| 92 | +import { PostgresServer } from '@queryleaf/postgres-server'; |
| 93 | + |
| 94 | +async function startServer() { |
| 95 | + // Connect to MongoDB |
| 96 | + const mongoClient = new MongoClient('mongodb://localhost:27017'); |
| 97 | + await mongoClient.connect(); |
| 98 | + |
| 99 | + // Start PostgreSQL server |
| 100 | + const server = new PostgresServer(mongoClient, 'your_database_name', { |
| 101 | + port: 5432, |
| 102 | + host: 'localhost', |
| 103 | + maxConnections: 100, |
| 104 | + }); |
| 105 | + |
| 106 | + // Server is now running |
| 107 | + console.log('PostgreSQL-compatible server started'); |
| 108 | + |
| 109 | + // To shutdown the server: |
| 110 | + // await server.shutdown(); |
| 111 | +} |
| 112 | + |
| 113 | +startServer().catch(console.error); |
| 114 | +``` |
| 115 | + |
| 116 | +## Use Cases |
| 117 | + |
| 118 | +The PostgreSQL wire-compatible server is useful for: |
| 119 | + |
| 120 | +- Connecting existing PostgreSQL-based applications to MongoDB |
| 121 | +- Using familiar SQL tools for data exploration and analysis |
| 122 | +- Providing a SQL interface to team members unfamiliar with MongoDB query syntax |
| 123 | +- Supporting applications that require a SQL interface but need MongoDB's document model |
0 commit comments