PostgreSQL Foundations·A database you can operate

Installing and connecting

Install PostgreSQL and learn how a client such as psql reaches the server that stores your data.

Intermediate 10 min read Lesson 1 of 300 Challenge included
What you will be able to do
  • 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

  1. 1Install PostgreSQL; it runs a server in the background, usually listening on port 5432.
  2. 2Open psql and connect to the default postgres database.
  3. 3Create a role for your app with login and a password.
  4. 4Create a database owned by that role.
  5. 5Connect to the new database using a connection string.
connect.sqlSQL
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.
Your turn

Create your first role and database

Easy

Create a login role named app with password 'secret', then a database named myapp owned by app.

SQL · starter
create role app;
Check your understanding before moving on.
Knowledge checkQuestion 1

Which statement best describes the difference between the server and a client?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 1 of 300 · 0% of course