A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.w3resource.com/PostgreSQL/snippets/connect-postgresql-docker-outside.php below:

Website Navigation


How to Connect to PostgreSQL in a Docker Container from Outside?

How to Connect to PostgreSQL in a Docker Container from Outside?Last update on December 23 2024 07:38:53 (UTC/GMT +8 hours)

Connecting to PostgreSQL in a Docker Container from Outside

To connect to a PostgreSQL database running in a Docker container from an external application or client, you need to configure the container’s network settings and expose the PostgreSQL port (usually 5432). This allows connections from the host machine or other networked devices.

Prerequisites

Steps to Connect to PostgreSQL in a Docker Container

Step 1: Start a PostgreSQL Docker Container with Port Exposed

When launching the PostgreSQL container, make sure to publish (-p) the container’s internal port (5432) to an external port on the host machine.

Example Command:

# Start a PostgreSQL container and expose port 5432
docker run --name my_postgres_container -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres
-p 5432:5432: Maps port 5432 on the Docker container to port 5432 on the host.
-e POSTGRES_PASSWORD=mysecretpassword: Sets the postgres user password.
-d postgres: Starts the PostgreSQL container in detached mode.

Step 2: Find the Host IP Address (If Needed)

If you’re connecting from another machine on the same network, you’ll need the host IP where the container is running. Use hostname -I to get the IP of the host machine:

# Get the host IP address
hostname -I

Step 3: Enable Listening for External Connections

By default, PostgreSQL in Docker may only listen to connections from localhost. Update the postgresql.conf file to allow connections from all IPs by setting listen_addresses to '*'.

Step 4: Configure pg_hba.conf for External Access

Modify the pg_hba.conf file to allow external connections by adding a rule for host access:

Step 5: Connect to PostgreSQL from Outside the Container

With PostgreSQL configured, you can now connect using psql or any PostgreSQL client. Specify the host’s IP and mapped port.

Example Command:

Explanation:

Full Example and Explanation

1. Start PostgreSQL Docker Container

Code:

# Start the container with the required port mapped to the host
docker run --name my_postgres_container -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres

2. Allow PostgreSQL to Listen on All IP Addresses

Code:

# Enter the container shell
docker exec -it my_postgres_container bash

# Configure PostgreSQL to listen on all IP addresses
echo "listen_addresses = '*'" >> /var/lib/postgresql/data/postgresql.conf

3. Configure pg_hba.conf for External Access

Code:

# Open pg_hba.conf and allow external connections
nano /var/lib/postgresql/data/pg_hba.conf

# Add the following line to allow all IP connections with md5 password authentication
host    all             all             0.0.0.0/0               md5

4. Restart PostgreSQL

Code:

# Restart the PostgreSQL service within the container
pg_ctl restart

5. Connect from an External Client

Code:

# Connect from an external PostgreSQL client
psql -h [host_ip] -U postgres -p 5432

Important Notes:

All PostgreSQL Questions, Answers, and Code Snippets Collection.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4