home

Postgres Snippets

Word count icon

102 words

Reading time icon

1 minutes

This document assumes you have postgres installed locally and are running psql. Always add a semicolon to the end of all commands.

Login

To login to to psql:

GNU Bash icon
psql -U postgres;

To login to psql and a database:

GNU Bash icon
psql -U postgres -d myDatabase;

Create a Database

psql works by allowing you to write SQL commands write in the command line. The database name needs to be camelCase To that end, to create a new database:

GNU Bash icon
CREATE DATABASE myDatabase;

List out stuff

To show all databases:

GNU Bash icon
\l;

To move into a database:

GNU Bash icon
\c myDatabase;

To show all tables in a database:

GNU Bash icon
\d+;

If you have data in your database you can view all rows with the SQL command:

Postgres icon
SELECT * FROM myDatabase;