2

I am new to postgresql and unable to create a database, please see my file below

My Docker compose file is :

version: '3.5'

services:
  postgres:
    container_name: postgres_container
    image: postgres
    environment:
      POSTGRES_USER: ${POSTGRES_USER:-postgres}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-changeme}
      PGDATA: /data/postgres
    volumes:
       - postgres:/data/postgres
    ports:
      - "5432:5432"
    networks:
      - postgres
    restart: unless-stopped
  
  pgadmin:
    container_name: pgadmin_container
    image: dpage/pgadmin4
    environment:
      PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
      PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
      PGADMIN_CONFIG_SERVER_MODE: 'False'
    volumes:
       - pgadmin:/root/.pgadmin

    ports:
      - "${PGADMIN_PORT:-5050}:80"
    networks:
      - postgres
    restart: unless-stopped

networks:
  postgres:
    driver: bridge

volumes:
    postgres:
    pgadmin:

The servers tab is empty,how to create a database,Do I need to enter something?

enter image description here

asd
  • 165
  • 10

1 Answers1

4

The pgAdmin needs a Server-config called servers.json.

More info on the servers.json can be found on their website

The config below worked for me:

docker-compose.yml

version: '3.9'

services:

  db:
    image: postgres:latest
    restart: "no"
    container_name: postgres
    volumes:
      - ./database:/var/lib/postgresql/data
    ports:
      - "9002:5432"
    environment:
      POSTGRES_PASSWORD: root
      POSTGRES_USER: root
      POSTGRES_DB: myDatabase

  pgadmin:
    image: dpage/pgadmin4
    container_name: pgadmin
    restart: "no"
    ports:
      - "9003:80"
    volumes:
      - ./servers.json:/pgadmin4/servers.json
    environment:
      PGADMIN_DEFAULT_EMAIL: you@email.com
      PGADMIN_DEFAULT_PASSWORD: admin123
      PGADMIN_LISTEN_PORT: 80
      POSTGRES_PASSWORD: root
      POSTGRES_USER: root
      POSTGRES_DB: myDatabase

networks:
  default:
    external:
      name: database_network

Database folder

Create a 'database' folder next to the above files because this is where the docker-compose wants to put the database.

servers.json

In the same folder as the docker-compose.yml

{
    "Servers": {
        "1": {
            "Name": "Local",
            "Group": "Servers",
            "Host": "db",
            "Port": 5432,
            "MaintenanceDB": "postgres",
            "Username": "root",
            "SSLMode": "prefer",
            "SSLCert": "<STORAGE_DIR>/.postgresql/postgresql.crt",
            "SSLKey": "<STORAGE_DIR>/.postgresql/postgresql.key",
            "SSLCompression": 0,
            "Timeout": 10,
            "UseSSHTunnel": 0,
            "TunnelPort": "22",
            "TunnelAuthentication": 0
        }
    }
}
BertC
  • 2,243
  • 26
  • 33
  • This works but will not have the password to connect to the server. https://stackoverflow.com/a/64626964/6165833 – ymoreau Nov 18 '21 at 10:29