You should have
- a running postgresql 17 or 16 install on your machine
- and/or pgAdmin installed
You connect to a postgres server
On that server you may have zero, one or multiple databases.
When you launch postgres on your local, you are actually launching the postgres server.
You connect and query the database on the command line using psql or in pgAdmin.
The postgres user is the super user.
It can do anything on the server, create and manage databases, users, drop everything etc … handle with caution.
Installing postgreSQL also creates a user with your machine user name : alexis in my case
The username (alexis) user is not as powerful as the postgres user
Attention, on Mac, there is no postgres user.
The superuser is your username
What are Privileges?
Privileges are permissions that control what actions a user can perform in PostgreSQL. Think of them as “rights” or “allowed actions” : like SELECT (reading data), INSERT (adding data), or CREATE (making new objects).
Similar to file permissions on a Linux system.
postgres user.SELECT (read), INSERT (add rows), UPDATE (modify), DELETE (remove rows).CONNECT and TEMP privileges on databases.CONNECT permission on a database to access it at all. Can be revoked to block access entirely.What is TEMP privilege?
TEMP (or TEMPORARY) privilege allows a user to create temporary tables in a database.
Example:
-- With TEMP privilege, you can do this:
CREATE TEMP TABLE my_temp_data (
id INT,
value TEXT
);
-- This table disappears when you disconnect
So when PUBLIC has TEMP privilege by default, it means any user who connects to the database can create temporary tables for their work session.
You can connect in the terminal or with pgAdmin
In the terminal you use psql to connect
psql takes the following parameters
-U username-h host (IP address or local host)-p port-d database_namePostgreSQL port is by default 5432 and does not have to be specified each time.
So to connect to a local instance of a postgres server with user bob and database database_name
psql -U bob -d database_name
To connect to a remote server IP: 123.456.789.246, same user and database
psql -U bob -h 123.456.789.246 -d database_name
to see your current connection status run
psql -c "\conninfo"
if it complains, add postgres as the database name
psql -c "\conninfo" -d postgres
This should return:
You are connected to database “postgres” as user “alexis” via socket in “/tmp” at port “5432”.

General tab:
Name: Give it a name like “Local PostgreSQL”
Connection tab:
Host name/address: localhostPort: 5432 (default PostgreSQL port)Maintenance database: postgresUsername: your_usernamePassword: Leave blank if you haven’t set oneto connect to a remote server, just change the connection tab parameters (host, user, password)

psql.exe fileit will open a terminal window and prompt you with hostname
Same with the other parameters
There are 2 configuration files for a postgres server
You have to restart your server everytime you modify these files
Find the location of the postgresql.conf file with show config_file; in a PSQL session

similarly for the pg_hba.conf file with show hba_file; in a PSQL session

General server configuration
This file controls most of the global settings for the PostgreSQL server. It includes:
Key points:
Example settings:
max_connections = 100shared_buffers = 128MBlog_destination = 'stderr'max_connections = 100
#101 gets rejected with too many connections errorshared_buffers = 128MB
work_mem = 4MB
Logging (for development/debugging)
log_statement = 'all': Logs every SQL query (useful for learning/debugging)log_line_prefix = '%t [%p]: ' : Adds timestamp and process ID to logslogging_collector = on: Enables log file collectionPerformance
effective_cache_size = 4GB: guide to query planner about total available memory. Set to ~50-75% of your RAMrandom_page_cost = 4.0: Set to 1.1 for SSD drives (makes queries faster on SSDs)When a line is commented out in postgresql.conf, PostgreSQL uses its built-in default value.
Role: Client authentication control
This file controls WHO can connect, FROM WHERE, to WHICH database, and HOW they authenticate.
“HBA” stands for “host-based authentication”.
It specifies:
Key points:
Example entries:
# TYPE DATABASE USER ADDRESS METHOD
local all postgres peer
host all all 127.0.0.1/32 md5
host production app_user 192.168.1.0/24 scram-sha-256
File Format (each line):
TYPE DATABASE USER ADDRESS METHOD
Common Entry Types:
# Local Unix socket connections
local all all trust # No password (dangerous!)
local all all md5 # Password required
local all all peer # OS username must match
# TCP/IP connections from localhost
host all all 127.0.0.1/32 md5 # IPv4 local
host all all ::1/128 md5 # IPv6 local
# Allow specific user to specific database
host myapp_db john 192.168.1.0/24 md5 # John from local network
# Allow connections from any IP (careful!)
host all all 0.0.0.0/0 md5 # Any IPv4
Connection Types:
Authentication Methods:
Important:
pg_ctl reload or SELECT pg_reload_conf();.psqlrc is a local configuration file for the psql command-line interface in PostgreSQL.
It allows you to customize your psql environment and set default behaviors.
~/.psqlrc on Unix-like systems%APPDATA%\postgresql\psqlrc.confalways timing the queries
\pset pager always\pset linestyle unicode\timing\x auto\set PROMPT1 '%[%033[1m%]%M %n@%/%R%[%033[0m%]%#\set HISTSIZE 2000\set HISTCONTROL ignoredups\set VERBOSITY verbose