|
| 1 | +--- |
| 2 | +title: "Cheatsheet: PostgreSQL" |
| 3 | +dateCreated: 2025-08-25 |
| 4 | +dateUpdated: 2025-08-25 |
| 5 | +--- |
| 6 | + |
| 7 | +This cheatsheet contains the steps I take to set up and interact with PostgreSQL, mainly when I'm developing something locally. It documents commands and flags that I use but forget when I haven't worked with PostgreSQL in a while. The commands below work on Arch Linux so they may need to be modified if you want to run them on a different system. They also assume that you're using the default host, localhost, and port, 5432. |
| 8 | + |
| 9 | +You don't have to go through steps 1-6 every time. You likely only need to go through a subset of them depending on your use case: |
| 10 | + |
| 11 | +- When setting up PostgreSQL for the first time, you probably want to do: 1, 2, 3, 4, 6. |
| 12 | +- When you have a new project to set up, you may want to do: 2, 3, 4, 6. |
| 13 | +- When you want to connect with a database via the command line, you can do: 2, 5, 6. |
| 14 | + |
| 15 | +## Steps |
| 16 | + |
| 17 | +1. Initialize cluster: |
| 18 | + |
| 19 | + ```console |
| 20 | + sudo --user postgres initdb --pgdata <path> |
| 21 | + ``` |
| 22 | + |
| 23 | + Or shorthand: |
| 24 | + |
| 25 | + ```console |
| 26 | + sudo -u postgres initdb -D <path> |
| 27 | + ``` |
| 28 | + |
| 29 | + **Notes:** |
| 30 | + |
| 31 | + - This is the bare minimum command to initialize a cluster, which is fine locally, but you probably want to set the authentication method, the bootstrap superuser's password, etc. for remote systems. |
| 32 | + - `--pgdata` or `-D` can be omitted if the `PGDATA` environment variable is set. |
| 33 | + |
| 34 | +2. Start server: |
| 35 | + |
| 36 | + ```console |
| 37 | + sudo --user postgres pg_ctl start --pgdata <path> --log <path> |
| 38 | + ``` |
| 39 | + |
| 40 | + Or shorthand: |
| 41 | + |
| 42 | + ```console |
| 43 | + sudo -u postgres pg_ctl start -D <path> -l <path> |
| 44 | + ``` |
| 45 | + |
| 46 | + **Notes:** |
| 47 | + |
| 48 | + - You may need to run `mkdir /run/postgresql` then `chown postgres:postgres /run/postgresql` once with root privileges before you can start the server. |
| 49 | + - `--pgdata` or `-D` can be omitted if the `PGDATA` environment variable is set. |
| 50 | + - `--log` or `-l` is optional but it's nice to have a logfile in case you need to check what happened. |
| 51 | + |
| 52 | +3. Create user: |
| 53 | + |
| 54 | + ```console |
| 55 | + sudo --user postgres createuser --superuser <username> |
| 56 | + ``` |
| 57 | + |
| 58 | + Or shorthand: |
| 59 | + |
| 60 | + ```console |
| 61 | + sudo -u postgres createuser -s <username> |
| 62 | + ``` |
| 63 | + |
| 64 | + **Notes:** |
| 65 | + |
| 66 | + - This step is optional if you plan to use the default `postgres` superuser. |
| 67 | + - Passing `--superuser` or `-s` when creating local users is for convenience, you probably need to be more careful with what privileges a user is granted on remote systems. |
| 68 | + - Pass `--pwprompt` or `-P` to `createuser` if you want to set a password for the new user. |
| 69 | + - For ease of use in running subsequent commands, you can set `<username>` to be the same as the current system user. |
| 70 | + |
| 71 | +4. Create database: |
| 72 | + |
| 73 | + If you created a superuser with the same username as the current system user and you want both the owner and database name to be the same: |
| 74 | + |
| 75 | + ```console |
| 76 | + createdb |
| 77 | + ``` |
| 78 | + |
| 79 | + Else, if you want a different database name: |
| 80 | + |
| 81 | + ```console |
| 82 | + createdb <dbname> |
| 83 | + ``` |
| 84 | + |
| 85 | + Else, you need to specify both the owner and database name: |
| 86 | + |
| 87 | + ```console |
| 88 | + sudo --user postgres createdb --owner <username> <dbname> |
| 89 | + ``` |
| 90 | + |
| 91 | + Or shorthand: |
| 92 | + |
| 93 | + ```console |
| 94 | + sudo -u postgres createdb -O <username> <dbname> |
| 95 | + ``` |
| 96 | + |
| 97 | + **Notes:** |
| 98 | + |
| 99 | + - `<dbname>` can also be omitted if the `PGDATABASE` environment variable is set, in which case, it will be the value used instead of the current system user's username. |
| 100 | + |
| 101 | +5. Connect to database: |
| 102 | + |
| 103 | + If the current system user's username is the same as the database name and owner's username: |
| 104 | + |
| 105 | + ```console |
| 106 | + psql |
| 107 | + ``` |
| 108 | + |
| 109 | + Else, you may need to pass one or both of the following: |
| 110 | + |
| 111 | + - `--dbname` or `-d` for the database name |
| 112 | + - `--username` or `-U` for the owner's username |
| 113 | + |
| 114 | + Once connected, you can do the rest with SQL. |
| 115 | + |
| 116 | + Some useful meta-commands: |
| 117 | + |
| 118 | + - `\du` to list users |
| 119 | + - `\l` to list databases |
| 120 | + - `\connect <dbname>` or `\c <dbname>` to connect to a different database |
| 121 | + - `\dt` to list tables in current database |
| 122 | + - `\quit` or `\q` to exit the shell |
| 123 | + |
| 124 | +6. Stop server: |
| 125 | + |
| 126 | + ```console |
| 127 | + sudo --user postgres pg_ctl stop --pgdata <path> |
| 128 | + ``` |
| 129 | + |
| 130 | + Or shorthand: |
| 131 | + |
| 132 | + ```console |
| 133 | + sudo -u postgres pg_ctl stop -D <path> |
| 134 | + ``` |
| 135 | + |
| 136 | + **Notes:** |
| 137 | + |
| 138 | + - `--pgdata` or `-D` can be omitted if the `PGDATA` environment variable is set. |
| 139 | + |
| 140 | +## References |
| 141 | + |
| 142 | +- <https://man7.org/linux/man-pages/man8/sudo.8.html> |
| 143 | +- <https://wiki.archlinux.org/title/PostgreSQL> |
| 144 | +- <https://www.postgresql.org/docs/current/app-initdb.html> |
| 145 | +- <https://www.postgresql.org/docs/current/app-pg-ctl.html> |
| 146 | +- <https://www.postgresql.org/docs/current/app-postgres.html> |
| 147 | +- <https://www.postgresql.org/docs/current/app-createuser.html> |
| 148 | +- <https://www.postgresql.org/docs/current/app-createdb.html> |
| 149 | +- <https://www.postgresql.org/docs/current/app-psql.html> |
0 commit comments