Skip to main content

Connecting to PostgreSQL Server

In this document, we are going to see how to connect and use a PostgreSQL Server in Selfmade Ninja Labs.

What is PostgreSQL?

PostgreSQL is a powerful, open source object-relational database system.

Advanced Database

PostgreSQL offers advanced features like JSON support, custom data types, advanced indexing, and powerful query optimization, making it ideal for complex applications.

Getting Started

Step 1: Access Services

Find the Services tab on the left window pane and then click on Services.

Next, find the PostgreSQL Server service and then click on Manage.

PostgreSQL Service Access

User and Database Setup

Create PostgreSQL User

In order for us to use PostgreSQL service, we need to create a User and a Database. Once you click on Manage, you will see the PostgreSQL service. Under Dashboard, click the "Create PostgreSQL User" and enter relevant details and click on Add User

Create PostgreSQL User

Create Database

Once you add the user, now it is time to create a Database. Click on Database, next to the Dashboard button and then enter the Database name.

Database Naming Convention

The database name follows the syntax 'username_databasename'.

Create PostgreSQL Database

Lab Environment Setup

Deploy Essential Lab

After creating the User and the Database, go to Machine Labs and then look for Essentials Lab and click on Dashboard.

Essential Lab Access

Once you open the dashboard, click on Deploy and then click on Confirm Deploy.

Deploy Lab Environment

Launch Development Environment

Once the service is deployed, you will have to click on the Code button.

Code Access

Here, you will be able to copy the Code Server Password for the current instance, copy that and then click on Launch IDE, which will open VS Code in another tab, where you can use the VS Code's terminal to connect to the PostgreSQL.

VS Code Launch

Development Setup

VS Code provides a complete development environment with PostgreSQL connectivity tools and database management capabilities.

Connecting to PostgreSQL

Get Connection Command

Once the VS Code editor opens in the new tab, go back to Labs, click on the Services button and then go to the Adminer service and then copy the VS Code Command (under Port Forwarding).

Adminer Port Forwarding

Execute Connection

Paste the command in the terminal of the VS Code editor and then hit enter. A popup will appear asking you to Open in Browser, click on the button.

Terminal Connection

Access Database Interface

This will open the application in another tab. Here, under System, select the PostgreSQL Service, and enter the relevant server name, username, password and the Database that you created, then hit Login.

PostgreSQL Login Interface

Connected

Once you hit login, this will open the PostgreSQL Server Service where you can perform different database commands.

Working with PostgreSQL

Once you hit login, this will open the PostgreSQL Server Service where you can perform different database commands.

PostgreSQL Management Interface

What You Can Do

FeatureDescription
Advanced QueriesComplex SQL queries with CTEs, window functions
JSON OperationsNative JSON and JSONB data type support
Custom FunctionsCreate stored procedures and custom functions
Advanced IndexingGIN, GiST, and other specialized index types
ExtensionsAdd functionality with PostgreSQL extensions
PostgreSQL Advantages
  • ACID Compliance: Full transaction support with strong consistency
  • Extensibility: Add custom data types, operators, and functions
  • Performance: Advanced query planner and optimization
  • Standards Compliance: Follows SQL standards more closely than other databases

Best Practices

  • Query Optimization: Use EXPLAIN ANALYZE to optimize query performance
  • Indexing Strategy: Implement appropriate indexes for your query patterns
  • Connection Pooling: Use connection pooling for production applications
  • Regular Maintenance: Perform VACUUM and ANALYZE operations regularly
Advanced Features

PostgreSQL supports advanced features like full-text search, geospatial data (PostGIS), and sophisticated data types that make it suitable for complex applications.