-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Postgres Cheat Sheet updates #260
Labels
documentation
Improvements or additions to documentation
Comments
PostgreSQL How Tocurl -sS https://webinstall.dev/ | bash
webi serviceman
webi postgres sudo env PATH="$PATH" \
serviceman add --system --path "$PATH" --username $(whoami) --name postgres -- \
postgres -D "$HOME/.local/share/postgres/var" -p 5432 /home/app/.local/share/postgres/var -p 5432 Access on Localhost: psql 'postgres://postgres:postgres@localhost:5432/postgres' vim ~/.local/share/postgres/var/postgresql.conf listen_addresses = 'localhost,10.0.0.100' vim ~/.local/share/postgres/var/pg_hba.conf # IPv4 local connections:
host all all 127.0.0.1/32 password
# IPv4 internal network connections:
host all all 10.0.0.1/16 password
host all all 192.168.0.0/24 password sudo systemctl restart postgres psql 'postgres://postgres:[email protected]:5432/postgres' Backup Databasepg_dump my_dbname > my_filename.sql pg_dump -Fc my_dbname > my_filename.pgdump Restore BackupFrom pg_restore --username postgres --no-owner --role=postgres -d postgres -1 ~/Downloads/postgres-yyyy-mm-dd.dump From psql < ./postgres-yyyy-mm-dd.sql |
mkdir -p ./Backups/
pg_dumpall \
--host localhost \
--database postgres \
--username postgres \
--file ./Backups/all."$(date +%Y-%m-%d)".sql
pg_dump \
--host localhost \
--username postgres \
--no-owner \
--quote-all-identifiers \
--no-privileges \
--schema-only \
dbname \
--file ./Backups/dbname.schema."$(date +%Y-%m-%d)".sql
pg_dump \
--host localhost \
--username postgres \
--no-owner \
--quote-all-identifiers \
--no-privileges \
--data-only \
dbname \
--file ./Backups/dbname.data."$(date +%Y-%m-%d)".sql touch ~/.pgpass
chmod 0600 ~/.pgpass
vim ~/.pgpass # hostname:port:database:username:password
localhost:5432:*:postgres:postgres |
Backup Heroku Database # heroku config:get -a <app-name> DATABASE_URL
heroku config:get -a foobar DATABASE_URL
The Heroku WayBackupmy_date="$(
date -u '+%F_%H.%M.%S'
)"
my_app='foobarapp'
heroku pg:backups:download -a "${my_app}" -o "postgres-${my_app}-${my_date}.dump" Delete Everything☣️ Caution! ☢️ Deletes EVERYTHING heroku pg:reset -a "${my_app}" Restore |
Ended up creating a backup/restore script pack: |
This was referenced Oct 17, 2023
This was referenced Sep 15, 2024
Merged
How to upgrade: # initialize a new db folder with the new version
echo "postgres" > /tmp/pwfile ; \
mkdir -p ~/.local/share/postgres/var-17.0 ; \
initdb -D ~/.local/share/postgres/var-17.0 --username postgres --pwfile "/tmp/pwfile" --auth-local=password --auth-host=password ; \
rm /tmp/pwfile
(
# run from /tmp (due to lax permissions)
cd /tmp/
# old and new arguments
PGPASSWORD="postgres" pg_upgrade -b ~/.local/opt/postgres-v12.3/bin/ -d ~/.local/share/postgres/var/ -D ~/.local/share/postgres/var-17.0/ -B ~/.local/opt/postgres-v17.0/bin/
)
# start the new server
~/.local/opt/postgres-v17.0/bin/postgres -D /Users/aj/.local/share/postgres/var-17.0/ -p 5432
# finish the migration
~/.local/opt/postgres-v17.0/bin/vacuumdb -U postgres --all --analyze-in-stages && \
./delete_old_cluster.sh && \
rm ./delete_old_cluster.sh
# move the data directory back to the default location
mv ~/.local/share/postgres/var-17.0/ ~/.local/share/postgres/var/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Create a Remote Group + Users
~/bin/pg-addgroup
:~/bin/pg-adduser
:How to add a remote user to
~/.pgpass
~/bin/pg-passwd
:How to Proxy PG through SSH
The PG server can lock down what the SSH user is allowed to do:
/home/pg-proxy/.ssh/authorized_keys
:An app account can run the SSH Proxy at system startup:
pg-register-ssh-proxy
:The ad-hoc alpine version:
(must use
\"
rather than'
, must prefix with usernameapp@
, must be started fromash
- notfish
, not inside ofscreen
)Note: add
-o ProxyCommand="'sclient --alpn ssh ${my_pg_host}'"
for ssh tunnels.How to route with SNI + ALPN
With
sslmode=require|verify-full
Postgres usesSSLRequest
(similar idea to StartTLS) before the real TLS connection. It begins with00 00 00 08 04 d2 16 2f
and does NOT include SNI or ALPN information until the characterS
is sent. Then normal TLS resumes.This means that the proxy must support StartTLS - or
sclient
oropenssl s_client
must be used to proxy the connection (which might as well usesslmode=disable
at that point).See traefik/traefik#7507
ULIDs
How to create a table with a Random ID
Auto-incrementing IDs are a bad idea. If you ever grow your database beyond a single instance it WILL cause problems.
Postgres, of course, being a good database, has a built-in function for using random IDs.
The problem, however, is that fully random IDs result in slow writes because the write index is always cold. ULIDs (above) solve this.
How to backup a database
Backup everything including permissions and such:
Backup a single database:
See also:
How to restore a database
Restore everything from
pg_dumpall
.gunzip -c backup.postgres postgres.gz | psql
Backup a single database:
gunzip -c my_database.postgres.gz | psql
See also:
How to export a CSV
Fields with commas will be double quoted. Fields that have double quotes will have those double quotes doubled.
See also:
\copy
section of https://www.postgresql.org/docs/9.5/app-psql.htmlHow to import a CSV
Note:
\copy
is different fromCOPY
.How to use TLS SNI
How to migrate to a new version
The text was updated successfully, but these errors were encountered: