Skip to content

Can I connect to a running PGLite server using e.g. DBeaver #49

@AllanLeanderRostockHansen

Description

Hi, I wonder if it's possible to connect to a running Py-PGLite database using a GUI like DBeaver? It could prove very useful for checking of the test data in the database is populated correcty.

I have attempted to do so using this code, but the connection times out.

"""
PGLite populated with mock data.
Connect to this using DBeaver?
"""

import asyncio
import json
import asyncpg

from py_pglite import PGliteConfig
from py_pglite import PGliteManager

async def main():
    """⚡ Instant PostgreSQL with asyncpg - proper configuration!"""

    # print("🚀 Starting py-pglite with asyncpg...")


    # Minimal config: TCP mode, custom port
    config = PGliteConfig(use_tcp=True, tcp_host="127.0.0.1", tcp_port=6543)

    with PGliteManager(config):
        print(f"PGlite started on {config.tcp_host}:{config.tcp_port}")
        print("\nConnect with:")
        print(f"  Host: {config.tcp_host}")
        print(f"  Port: {config.tcp_port}")
        print("  Database: postgres")
        print("  User: postgres")
        print("  Password: postgres\n")

        conn = await asyncio.wait_for(
            asyncpg.connect(
                host=config.tcp_host,
                port=config.tcp_port,
                user="postgres",
                password="postgres",
                database="postgres",
                ssl=False,
                server_settings={},
            ),
            timeout=10.0,
        )

        # Create table
        await conn.execute("""
            CREATE TABLE IF NOT EXISTS demo (
                id SERIAL PRIMARY KEY,
                name TEXT NOT NULL,
                info JSONB
            )
        """)

        # Insert mock data
        mock_data = [
            ("Alice", json.dumps({"role": "admin", "score": 95})),
            ("Bob", json.dumps({"role": "user", "score": 80})),
            ("Carol", json.dumps({"role": "moderator", "score": 88})),
        ]
        await conn.executemany(
            "INSERT INTO demo (name, info) VALUES ($1, $2)",
            mock_data,
        )

        print("Database is running. Press Ctrl+C to stop.")
        try:
            while True:
                await asyncio.sleep(60)
        except KeyboardInterrupt:
            print("\nStopped by user.")
        finally:
            await conn.close()


if __name__ == "__main__":
    asyncio.run(main())

This is my DBeaver settings:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions