This docker-compose file spins up a PostgreSQL database along with an instance of PgAdmin4 which can be accessed from a browser
The files in the sql
directory are called dump files.
- Each file reflects a different SQL database
- The
init.sh
file goes through each file, creates the corresponding database in PostgreSQL, and runs the contents of that SQL file against the corresponding database
[! NOTE] Initial SQL files in this repo were generated by ChatGPT
Rename the .env-example
file to .env
and update the username and password credentials
cd
into directory containinginit.sh
file- Make sure that the
init.sh
file is executable. If it is not, usechmod +x init.sh
- Execute the
init.sh
file
./init.sh
init.sh
does the following things:
- Loads
.env
file to gain access to environmental variables (needed by theinit.sh
file itself) - Initializes
docker-compose.yaml
file viadocker compose up -d
- Creates PostgreSQL databases and loads into them the dump files as defined by .sql files in the
sql
directory- Each database name is the same as the
sql
file name
- Each database name is the same as the
docker compose down -v
NOTE: -v
option removes any created volumes (while optional, it is recommended)
docker exec -it <container name> bash
psql -U <POSTGRES_USER> <POSTGRES_DB>
1st step accesses bash shell within the docker db container 2nd step accesses psql db from within the terminal
NOTE: <POSTGRES_USER>
and <POSTGRES_DB>
are specified by the .env
file
NOTE: To exit the bash cli, use CTRL
+ d
- Navigate to
http://localhost:<PGADMIN_PORT>
(from.env
file) - You will be prompted to login with username and password. Use .env credentials
<PGADMIN_DEFAULT_EMAIL>
AND<PGADMIN_DEFAULT_PASSWORD>
- Add a "Name" to your database under the "General" tab
- Click on "Add New Server" in pgAdmin homepage
- In the "Connection" tab, enter the following information:
a. Host name/address --
db
(The name of database service defined indocker-compose
) b. Port --<DB_PORT>
(5432
is the default PostgreSQL port number) c. Maintenance database --<POSTGRES_DB>
(The name of the database specified in .env file) d. Username --<POSTGRES_USER>
e. Password --<POSTGRES_PASSWORD>
- Click "Save" to save the server configuration
- Your PostgreSQL server should now appear under the "Servers" section in homepage
- Click on the server name to expand the database tree and view your databases, tables, etc.
NOTE: The sql files in this starter template were generated by ChatGPT