In this post i will talk about some Postgres basics.
I will assume you have already installed Postgresql.11+ and that you have basic terminal skills.
Utilities
First, navigate into the postgres directory where it exists, you'll find many files and directories, but we're concerned with the bin/
directory, which is where you'll find postgres utilities that we'll be using.
Clusters
Next, run the initdb
utility inside the bin/
directory and provide the name for your database cluster using the -D flag
bin/initdb -D your-cluster-name
A Database Cluster is a collection of databases. Each cluster is managed by a PostgreSQL server which runs on a single host.
Clusters are stored as a directory referred to as base directory. You can refer to my guide for more information if you're interested.
This will initialize a database cluster which we will be using to create databases.
pg_ctl
The pg_ctl
utility is used to manage Postgres servers and processes.
Run bin/pg_ctl -D your-cluster-name
to start the Postgres server for the cluster you just created. By default, Postgres will use port 5432
psql
The psql
utility is used to start an interactive shell with your database cluster so that you can use SQL like you would with any other DBMS.
Run bin/psql -l
to list all available databases in your cluster. In Postgres version 11+ clusters are created with 3 default databases: Postgres, Sample0, Sample1
Run bin/psql postgres
.
- you can exit this shell using \q
- type 'help' to see the help prompt
SQL commands
Let's create a new database using the command
CREATE DATABASE your-database-name;
(Don't forget the semicolon!)
now run \l
to view all the databases again (from inside the shell this time), you can see the new database was created.
Now instead of exiting and reconnecting to the new database, you can instead use \c your-database-name
to directly switch to it.
This should be enough to get you started exploring Postgres.
Top comments (0)