DEV Community

Cover image for Define a PostgreSQL database connection in JSON and import it in PGAdmin 4
Francesco Tisiot
Francesco Tisiot

Posted on • Originally published at ftisiot.net

2 1

Define a PostgreSQL database connection in JSON and import it in PGAdmin 4

Yesterday I updated my PGAdmin 4 and found an interesting option to manage database connection via the Tools -> Import/Export Servers, allowing to pass almost all the parameters required (it doesn't export/import the password) to connect to a database via JSON files.

The question is: how can I define a connection in JSON so it can pick it up and imported in PGAdmin 4? It's actually pretty simple, all you have to do is, substitute all the parameters in the following example

{
    "Servers": {
        "1": {
            "Name": "CONNECTION_NAME",
            "Group": "GROUP_NAME",
            "Host": "HOSTNAME",
            "Port": PORT,
            "MaintenanceDB": "DATABASE_NAME",
            "Username": "USERNAME",
            "Password": "PASSWORD",
            "SSLMode": "SSLMODE"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Where

  • CONNECTION_NAME is the logical name you want to give to the connection
  • GROUP_NAME is the logical group name that you can use to group more connections
  • HOSTNAME, PORT, DATABASE_NAME, USERNAME, PASSWORD, SSLMODE are the usual parameters to connect to the database.

You can also add more than one database connection in the mix, E.g.

{
    "Servers": {
        "1": {
            "Name": "CONNECTION_NAME1",
            "Group": "GROUP_NAME1",
            "Host": "HOSTNAME1",
            "Port": PORT1,
            "MaintenanceDB": "DATABASE_NAME1",
            "Username": "USERNAME1",
            "Password": "PASSWORD1",
            "SSLMode": "SSLMODE1"
        },
        "2": {
            "Name": "CONNECTION_NAME2",
            "Group": "GROUP_NAME2",
            "Host": "HOSTNAME2",
            "Port": PORT2,
            "MaintenanceDB": "DATABASE_NAME2",
            "Username": "USERNAME2",
            "Password": "PASSWORD2",
            "SSLMode": "SSLMODE2"
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Import the JSON definition in PGAdmin 4

Once you have the above config stored in a JSON file, you can then import in PGAdmin4 with:

  • select Tools -> Import/Export Servers
  • select Import
  • select the connections you want to import and click on Next
  • click on Finish

And done! you imported all your JSON connections

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay