Fortgeschritten8 minAktualisiert: 2026-09-13

PostgreSQL 16: Setup, Roles, and Remote Connections

Connect your application to a PostgreSQL service, get roles and schema privileges right, and take backups with pg_dump.

Geprüft mit

PostgreSQL
16
Client
psql 16 / pgAdmin 4
Zuletzt geprüft
2026-09-13
Auf dieser Seite
Alle Anleitungen
Lesefortschritt

Unlike MySQL, PostgreSQL keeps role privileges and database privileges separate. This is where most people get stuck: letting a role connect to a database does not mean it can create tables in it. This guide creates an application role from scratch and grants only what it needs.

First connection and the superuser#

When you create the service, the initial user and password are set through environment variables; you can see them under 'Startup Variables' on the service detail page. Make the first connection with that user, then create a separate role for your app -- running your app as superuser turns a single SQL injection bug into the loss of the whole cluster.

Yerel terminal
# Panelde yazan IP ve portu kullanin
psql "postgresql://postgres:[email protected]:5432/postgres"

# Baglanti kuruldu mu?
postgres=# SELECT version();

A dedicated role and database for the app#

psql
-- 1) Rolu olustur (LOGIN olmadan baglanamaz)
CREATE ROLE uygulama WITH LOGIN PASSWORD 'guclu-bir-parola';

-- 2) Veritabanini bu rolun sahipligiyle olustur
CREATE DATABASE uygulama_db OWNER uygulama;

-- 3) Herkese acik public semasindan yazma yetkisini al
\c uygulama_db
REVOKE CREATE ON SCHEMA public FROM PUBLIC;

-- 4) Yalnizca uygulama rolune ver
GRANT ALL ON SCHEMA public TO uygulama;

Keeping remote access safe#

  • Instead of exposing the port to the world, allow only your application server's IP.
  • Keep the password in the service's environment variables, not in application code.
  • Use different roles and passwords per environment; sharing one password means a leak in staging is a leak in production.
  • For read-only consumers, create a separate role limited to SELECT.

Taking backups with pg_dump#

Backup and restore

Yerel terminal
# Ozel bicim (-Fc): sikistirilmis ve secici geri yuklemeye uygun
pg_dump -Fc \
  -h 192.0.2.10 -p 5432 -U uygulama \
  -d uygulama_db \
  -f uygulama_db_$(date +%F).dump

Fehlerbehebung

Symptom

connection refused / timeout

Ursache

The service is stopped, the wrong port is used, or PostgreSQL only listens on localhost.

Lösung

Confirm the service is running and check the port on the 'Address' card; make sure listen_addresses is set to '*'.

Symptom

permission denied for schema public

Ursache

PostgreSQL 15+ behaviour: the role can connect but has no CREATE privilege on the schema.

Lösung

Run GRANT ALL ON SCHEMA public TO <role>; while connected to that database.

Symptom

too many clients already

Ursache

The app opens a new connection per request and never closes it, filling max_connections.

Lösung

Use a connection pool in the application and keep the pool size below max_connections.

Häufige Fragen

Should I migrate from MySQL to PostgreSQL?

If you already have a working MySQL setup, migrate only for a concrete reason: JSONB indexing, advanced window functions, geospatial data (PostGIS) or strict typing. The migration itself has a cost, and both engines are more than enough for a typical web app.

Why can't I create a database from the panel?

PostgreSQL runs as its own service here; you create databases with CREATE DATABASE in psql rather than through the panel. The steps in this guide follow exactly that path.

War diese Anleitung hilfreich?

Feedback geht direkt an das Team, das diese Anleitung pflegt.

Verwandte Anleitungen