Start local PostgreSQL
When docker-compose.yml is in the root folder
When docker-compose.yml is in the nested folders
docker-compose -f ./postgresql/docker-compose.yml up
Access Adminer
- Go to
http://localhost:8080to access Adminer portal. - Enter respective credentials after selecting system as
PostgreSQLand hit login - You are connected to postgresql GUI and good to go now!
Run commands inside the container
Run the following command and hit enter to know version of the PostgreSQL:
docker-compose -f postgresql/docker-compose.yml run db psql -U chaiaurcode -p 5432 -h db -d chaiDB -c "SELECT version();"-
-f postgresql/docker-compose.yml: points to the file which we want to run docker compose on. If we havedocker-compose.ymlin root folder we don't need to pass this flag. -
run db: Runs a one-time command in thedbservice defined in the Docker Compose file. -
psql: Initiates the PostgreSQL command-line client. -
-U chaiaurcode: Specifies the PostgreSQL username aschaiaurcode. -
-p 5432: Specifies the port number (5432) on which the PostgreSQL server is running. If the port is5432then this flag is optional. -
-h db: Specifies the host asdbwhere the PostgreSQL server is located. -
-d chaiDB: Specifies the name of the PostgreSQL database aschaiDB. -
-c "SELECT version();": Executes the SQL commandSELECT version();on the specified database.
Running this command will prompt you to enter the password. Enter POSTGRES_PASSWORD env var value from docker compose file. And, you are good to go!
Output after correct password:
version --------------------------------------------------------------------------------------------------------------------------- PostgreSQL 16.1 (Debian 16.1-1.pgdg120+1) on aarch64-unknown-linux-gnu, compiled by gcc (Debian 12.2.0-14) 12.2.0, 64-bit (1 row)
Now, you can run postgresql related commands here.