brew install postgresql
Here's a nice readable tutorial on creating local Postgres users and DBs.
Create a user myuser
with permissions to administrate a database mydatabase
:
psql postgres
# Inside psql shell as `postgres` user
CREATE ROLE myuser WITH LOGIN PASSWORD 'my_super_secret_password';
ALTER ROLE myuser CREATEDB;
\q
# Go back into psql shell as `myuser` user
psql postgres -U myuser
# In psql shell as `myuser` user
CREATE DATABASE mydatabase;
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myuser;
\q
You can now shell into that DB with psql -U myuser -d mydatabase
.
If you don't already have passwordless login and want it, you can set your pg_hba.conf
to trust a user (or all users) on localhost.
https://www.postgresql.org/docs/14/auth-trust.html
# The -a flag prints the SQL lines to stdout
psql -U myuser -d mydatabase -a -f commands.sql
Dump DB to file without explicitly setting ownership:
pg_dump mydatabase --no-owner > dumpfile.postgres
Load a DB dump:
psql -U myuser -d mydatabase < dumpfile.postgres
(You can also see the full list by entering \?
in psql
shell, but some are more frequently used than others.)
\q - quit
\d - list tables, views, and sequences
\du - list roles
\dt - list tables
\l - list databases