Langkau ke kandungan
Localtonet Localtonet Papan Pemuka

Halaman ini belum tersedia dalam bahasa Bahasa Melayu. Ia dipaparkan dalam bahasa Inggeris.

Reach a PostgreSQL server through Localtonet

Bantuan untuk halaman ini
Dalam halaman ini 9 bahagian
  1. How it works
  2. Before you start
  3. Create the tunnel
  4. Open the TCP - UDP page
  5. Fill in the form
  6. Start the tunnel and copy the address
  7. Connect from outside
  8. PostgreSQL settings that matter behind a tunnel
  9. listen_addresses can stay local
  10. pg_hba.conf decides by the client's address, and that is the Localtonet client
  11. Keep the same address
  12. Secure it
  13. Common problems
  14. Related pages

Connect to a PostgreSQL server on your machine from another network with psql, pgAdmin, DBeaver or your application: a TCP tunnel gives port 5432 a public host name and port.

How it works #

The Localtonet client runs on the machine that hosts PostgreSQL, or on another device in the same network. A TCP tunnel points to the PostgreSQL port, 5432. Localtonet gives the tunnel a public address made of a host name and a port. A PostgreSQL client that connects to that address reaches your server. The details of TCP tunnels are on the TCP page.

Check pg_hba.conf before you start the tunnel. Connections through the tunnel reach PostgreSQL from the Localtonet client, which is a local address. If pg_hba.conf has a trust rule for 127.0.0.1, anyone who reaches the tunnel is let in without a password. See the settings below.

Before you start #

  • A Localtonet account and the Localtonet client connected with your AuthToken. See Quick start.
  • PostgreSQL running on the device. Check that psql -h 127.0.0.1 -p 5432 -U <DB_USER> -d <DB_NAME> works on the device itself and asks for a password.
  • A PostgreSQL role for remote use, with a strong password and only the privileges it needs. Don't use the postgres superuser.

Create the tunnel #

  1. Open the TCP - UDP page #

    Open My Tunnels › TCP - UDP and expand Create TCP - UDP Tunnel.

  2. Fill in the form #

    FieldWhat to enter
    ProtocolTCP
    Device tokenThe token the Localtonet client on the device uses.
    Server regionChoose a region close to your device for lower latency.
    Local IP address127.0.0.1 when PostgreSQL runs on the same device as the client. For a server on another machine in the network, enter that machine's address.
    Local port5432, or the port value in postgresql.conf.
    URL typeRandom subdomain. Custom subdomain lets you choose the name and needs Balance.

    Select Create tunnel.

  3. Start the tunnel and copy the address #

    The tunnel appears under Your tunnels. Select Start. The URL column shows the public address as host:port, and the Server Port column shows the port on its own.

Connect from outside #

With psql, the host goes after -h and the port after a lower-case -p. psql asks for the password.

Terminal
psql -h <TUNNEL_HOST> -p <TUNNEL_PORT> -U <DB_USER> -d <DB_NAME>

Applications usually take a connection URI. sslmode=require makes the client refuse an unencrypted session:

Connection URI
postgresql://<DB_USER>:<DB_PASSWORD>@<TUNNEL_HOST>:<TUNNEL_PORT>/<DB_NAME>?sslmode=require

In pgAdmin or DBeaver, enter the tunnel's host name in the host field and the tunnel's port in the port field.

PostgreSQL settings that matter behind a tunnel #

listen_addresses can stay local #

The Localtonet client connects to PostgreSQL from your own device. PostgreSQL doesn't have to listen on a public interface: the default listen_addresses = 'localhost' is enough when the client runs on the same machine. Leave it that way, so the tunnel is the only path from outside.

pg_hba.conf decides by the client's address, and that is the Localtonet client #

PostgreSQL picks the authentication method from pg_hba.conf by the address a connection comes from. Through the tunnel, that address is the Localtonet client: 127.0.0.1 when the client runs on the PostgreSQL machine. The matching rule must ask for a password:

pg_hba.conf
# TYPE  DATABASE   USER        ADDRESS         METHOD
host    <DB_NAME>  <DB_USER>   127.0.0.1/32    scram-sha-256

Replace any trust rule for 127.0.0.1/32 or ::1/128 and reload PostgreSQL. If the client runs on another device, use that device's address in the rule, and set listen_addresses so that PostgreSQL also listens on its own network address.

For the same reason, address rules in pg_hba.conf can't tell visitors apart. Limit the visitors on the tunnel instead, with IP Restriction.

Keep the same address #

The host name stays with the tunnel. The port is assigned by the server when the tunnel starts. With Is Reserved? on in the tunnel's Settings, the port stays the same when the tunnel or the Localtonet client restarts, which is what a connection URI in an application needs. With it off, the port changes on a restart. Tunnels that you create while your account has Balance start with Is Reserved? on. Turning it on needs Balance and a started tunnel.

With Balance you can also choose the port under Server Port › Change, for example 5432 if no other tunnel on that server uses it. See Ports.

Free plan. A TCP tunnel stops after 30 minutes on the Free plan and can be started again. The port can be different after each start. See Plans and Balance.

Secure it #

  • IP Restriction. In the tunnel's Settings, select IP Restriction › Manage, choose Allow only these and add the addresses of the applications and people that connect. Use my IP fills in your current address. Changes apply right away. IP Restriction needs Balance. See Access control.
  • Password authentication for local addresses, as described above, and a dedicated role without superuser rights.
  • TLS in PostgreSQL. Tunnels are not encrypted end to end by default, so let PostgreSQL encrypt the session: set ssl = on with a certificate on the server and connect with sslmode=require.
  • Limits. Expiration Date, Bandwidth Limit and Speed Limits in the tunnel's Settings cap how long and how much the tunnel can be used. See Limits and expiration.
  • Request Log › Show lists the connections made to the tunnel with the visitor's IP address.

Common problems #

psql: connection refused, or the connection times out

Check that the tunnel is started and its Connection column shows ON, that you use the current port from Server Port, and that PostgreSQL answers on the device at the tunnel's Local IP address and Local port. If you use IP Restriction, check that your current address is in the list.

FATAL: no pg_hba.conf entry for host "127.0.0.1"

The tunnel works and PostgreSQL answered. No rule in pg_hba.conf matches the Localtonet client's address together with your user and database. Add the rule shown above and reload PostgreSQL.

FATAL: password authentication failed for user "…"

The tunnel works. Check the role's password, and that the role has one: a role created without a password can't sign in with scram-sha-256.

I was let in without a password

A trust rule matches the Localtonet client's address. Stop the tunnel, change the rule to scram-sha-256, reload PostgreSQL, then start the tunnel again.

The application lost its connection after a restart

The port is not reserved, so it changed when the tunnel or the client restarted. Turn on Is Reserved?, or update the port in the application.

The tunnel stopped by itself after half an hour

On the Free plan a TCP tunnel stops after 30 minutes. Select Start to begin another session, or add Balance to remove the session limit.

"You need to purchase balance to use this feature."

Is Reserved?, Server Port, IP Restriction and Custom subdomain need Balance. Add it under Balance.

Sokongan