Installing and connecting
Install PostgreSQL and learn how a client such as psql reaches the server that stores your data.
- Know that the server holds the data and the client asks for it.
- Connect with psql using a connection string.
- Create a role for your app with login and a password.
- Create a dedicated database owned by that role.
In plain English
The PostgreSQL server is the background program that actually stores your data, while a client is any program, such as psql, that connects to it and asks for data. psql is the official command-line client you open in a terminal to type SQL. A connection string packs everything the client needs into one short line: the host, the port, the database name, the user and the password. A role is a database user account, and the database is the named container where your tables live. Installing PostgreSQL gives you a server on your own machine and a psql client to talk to it.
Step by step
- 1Install PostgreSQL; it runs a server in the background, usually listening on port 5432.
- 2Open psql and connect to the default postgres database.
- 3Create a role for your app with login and a password.
- 4Create a database owned by that role.
- 5Connect to the new database using a connection string.
create role app login password 'secret';
create database myapp owner app;Common mistakes
- Doing normal work in psql as the superuser (often named postgres) is risky, because that account can change or drop anything.
- Forgetting that the server and the client are separate programs causes confusion, because the server must already be running before psql can connect.
- Storing your app data in the default postgres database mixes it with system data, so create a dedicated database instead.
- Committing a connection string with a real password into source control leaks a credential.
Create your first role and database
Create a login role named app with password 'secret', then a database named myapp owned by app.
create role app;Which statement best describes the difference between the server and a client?
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Lock in what you just learned.
Mark this lesson complete to save your place and move one step closer to the certificate.