Sub topic 1
What PostgreSQL Is and How Clients Talk to It
Prerequisites
You should be comfortable using a terminal or command prompt. You should also know that a database stores organized information and that a table stores related records in rows and columns. This chapter assumes PostgreSQL is installed or that you are ready to install it in the next chapter.
PostgreSQL Has Two Main Parts
PostgreSQL is a relational database management system. It stores structured data in tables and understands SQL, the language used to define, read, and change that data.
A PostgreSQL installation normally includes a server and one or more clients:
- The server is the long-running program that stores databases, executes SQL, and manages connections.
- A client is a program that sends requests to the server and displays its responses.
psqlis PostgreSQL's text-based command-line client.
The server does the database work. The client provides a way to ask for that work. Keeping these roles separate lets many clients connect to one server, including terminal programs, desktop tools, web applications, and services.
What Happens During a Connection
When psql connects, it needs enough information to identify the server and the account that will use it. The main connection values are:
| Value | Meaning | Typical local value |
|---|---|---|
| Host | Computer running PostgreSQL | `localhost` |
| Port | Network endpoint used by the server | `5432` |
| User | PostgreSQL role used for authentication | `postgres` |
| Database | Database to open after authentication | `postgres` |
localhost means the same computer where the client is running. Port 5432 is PostgreSQL's default port, although an administrator can configure another port.
A PostgreSQL role is an identity recognized by the server. A role can log in and can have permissions on databases and tables. The role name is separate from your operating-system username, even though PostgreSQL can be configured to use matching names.
A database is a named container inside a PostgreSQL server. A server can manage several databases. A connection uses one database at a time, and SQL queries normally work with objects in that selected database.
> Note > The server must be running before a client can connect. Installing PostgreSQL places the software on your computer, but it does not guarantee that the server is currently accepting connections.
A First Client Check
After installation, check that the client exists and that the server responds:
psql --version
pg_isreadyThe first command prints the installed psql version. The second asks whether a PostgreSQL server is accepting connections on the default local endpoint. A successful readiness response confirms that a server is reachable, but it does not prove that a particular role can authenticate to a particular database.
A complete connection attempt supplies explicit values:
psql -h localhost -p 5432 -U postgres -d postgresAt the password prompt, enter the password for the postgres role if one is configured. Once connected, psql displays a prompt such as postgres=#. The prompt indicates that SQL entered there will be sent to the selected database.
Exercise
Which component stores data and executes SQL, and which component sends SQL from a terminal?
Show answer
The PostgreSQL server stores data and executes SQL. The psql client sends SQL and displays the server's responses.