- Install -
brew install postgresql@14- At successful installation this message - 'This formula has created a default database cluster with: initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres'
brew services list- See which versions of postgresql have been installed and which version is running now- Start & Stop automatically now & on login
- Start -
brew services start postgresql@14 - Stop -
brew services stop postgresql@14 - Restart -
brew services restart postgresql@14
- Start -
- To switch versions:
brew services stop postgresql@14brew services start postgresql@13brew unlink postgresql@14brew link postgresql@13echo 'export PATH="/opt/homebrew/opt/postgresql@13/bin:$PATH"' >> ~/.zshrc
- Initial setup
createuser -s postgres- create user postgres- By default a user is also created with the home user name i.e.
shaunthomas
- By default a user is also created with the home user name i.e.
psql -U postgres- login postgres user
- Binaries are in
/opt/homebrew/opt/postgresql@14/bin- Default binaries
postgresandpsqlare in/opt/homebrew/bin
- Default binaries
- Run not as daemon
/opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14 - References
pg_ctl -D /usr/local/var/postgres startpg_ctl -D /usr/local/var/postgres stoppg_ctl -D /usr/local/var/postgres statuspg_ctl -D /opt/homebrew/var/postgresql@14 status
-D- data directory- If installed by @14 brew install command then data directory is in
/opt/homebrew/var/postgresql@14 - More options > https://www.postgresql.org/docs/current/app-pg-ctl.html
- Run commands
docker pull postgres:16-alpinedocker run --name postgres16 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 -d postgres:16-alpinedocker exec postgres16 psql -U postgres -c "CREATE DATABASE testdb" postgres
- Now connect from Intellij IDEA
!! Make sure Homebrew is not running Postgresql on the same port locally !!
- docker-compose
- https://hub.docker.com/_/postgres
- https://docs.docker.com/engine/examples/postgresql_service/
- Good ref - docker-compose - Postgresql + pgAdmin - https://github.com/docker/awesome-compose/tree/master/postgresql-pgadmin
- Dockerfile - https://github.com/docker-library/postgres/blob/4edbda205c684c861e6fbf964de5d00845864d42/12/alpine/Dockerfile
- Default port - 5432
- Default volume -
/var/lib/postgresql/data - PSQL command -
psql -h localhost -p 5432 -d postgres -U postgres --password - Ref:
- Download and install PostgreSQL app from https://postgresapp.com/
- Start the app and click Initialize button
- Then in the database shown, click on the one with name 'postgres'
- https://www.pgadmin.org/
- Download or brew install --cask
- Mac native app
- Not that many features as pgAdmin
- More commerical than open source
- https://dbeaver.io/
- Eclipse based
- Start pgAdmin
- Create Role
- Right-click 'Login/Group Roles' and select 'Create / Login/Group Role'
- Enter 'Name' on the 'General' tab
- Enter 'Password' on the 'Definition' tab
- Select all privileges on the 'Privileges' tab
- Click 'Save'
- Create Database
- Right-click 'Databases' and select 'Create / Database'
- Enter 'Database' on the 'General' tab
- Enter 'Owner' on the 'General' tab
- Click 'Save'
psql -U postgres- Start psqlCREATE ROLE "test-user" WITH LOGIN PASSWORD 'testdb';CREATE DATABASE "test-db" OWNER "test-user";CREATE SCHEMA "test-schema" AUTHORIZATION "test-db";
CREATE ROLE user_name SUPERUSER LOGIN PASSWORD 'password';- or
ALTER USER user_name WITH SUPERUSER;
\q- quit\l- list all database\du- display all users & roles\c(\connect) - show current database or connect to databasee.g. \c greetings-db
- Tip: By default PSQL will show database name in the prompt
- Location with brew install -
/opt/homebrew/Cellar/postgresql@14/14.5_5/bin/pg_dump
- https://www.postgresqltutorial.com/
- See the menu links on the right side
- To see all server settings -
SELECT * FROM pg_settings; - Might have to reload/restart after config change*
SHOW datestyle;- See default datestyle. Default (ISO, MDY). Here MDY means MM-DD-YYYY. ISO is YYYY-MM-DDSET datestyle = 'ISO, YMD';
- pgbadger - memory leak in database connection
- max_connections
ALTER SYSTEM SET max_connections = <value>SHOW max_connections;- Change
psql -U <admin_user> -d <database_name>ALTER SYSTEM SET max_connections = 500;SELECT pg_reload_conf();- To reload values in/opt/homebrew/var/postgresql@14/postgresql.conffile
- Other option
SELECT name, setting
FROM pg_settings
WHERE name = 'max_connections';- pg_stat_activity
SELECT * FROM pg_stat_activity;- Find process running for more than 5 minutes
SELECT pid, query, state, age(now(), query_start) AS "age"
FROM pg_stat_activity
WHERE state != 'idle' AND query_start < now() - interval '5 minute';- Terminate process running for more than 5 minutes
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'active'
AND now() - query_start > interval '5 minutes';
//-- pg_cancel_backend(pid)
//-- state = 'idle'
//-- state != 'idle'- To find all blocking PIDs
select pid,
usename,
pg_blocking_pids(pid) as blocked_by,
query as blocked_query
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;- to terminate all these PIDs replace
pidin the select query withpg_terminate_backend(pid)
- UUID generation -
uuid_in(overlay(overlay(md5(random()::text || ':' || random()::text) placing '4' from 13) placing to_hex(floor(random() * (11 - 8 + 1) + 8)::int)::text from 17)::cstring) - Timestamp -
NOW()::timestamp