Install and setup Postgres using Docker locally
Make sure Docker Desktop is running.
Pull a specific version of postgres
from Docker Hub.
docker pull postgres:16
Create and then run a postgres
container in detached mode.
docker run --name <container_name> -d -p 5432:5432 \
-e POSTGRES_PASSWORD=<password> postgres:<tag>
Check if the container is really running.
docker ps
Connect to the postgres
container. postgres
is the default username
—
you can customize this if you want.
docker exec -it <container_name> psql -h <hostname> -U postgres
Create a new database.
postgres=# CREATE DATABASE <db_name>;
List all existing databases.
postgres=# \l
# or
postgres=# \list
Create a new table via psql
or through your app.
Connect to an existing database.
postgres=# \c <db_name>
# or
postgres=# \connect <db_name>
List the tables of an active database.
<db_name>=# \dt