[Docker] WSL2 => Docker Desktop => Postgresql


use command to run postgresql and pgadm4 in WSL2 Docker-Desktop

up postgres
    >> docker run --name {containerName} -d -p 9030(windows port):5432(postgres default port) -e POSTGRES_PASSWORD={password} postgres:15.3(version)

up pgadmin4
    >> docker run -d -p 9031(windows port):80(pgadmin default port) --name {containerName} -e "PGADMIN_DEFAULT_EMAIL={email@abc.com}" -e "PGADMIN_DEFAULT_PASSWORD={password}" dpage/pgadmin4

Create DB from pgadmin4:
    docker inspect container ip: (pgadm4 connect to postgres, container link to docker layer and ponit to the port of postgres)     port: the docker mapped port of postgres container


Enter postgres with WSL2:
    docker exec -it {container ID} bash
    psql -U postgres
    or
    docker exec -it postgres-poolpay psql -U postgres(USER)

golang:
    //the parameter of host should fill the localhost in WSL2 which is the network range subnet Docker containers using
     host: wsl2.localhost(docker)  = ipconfig(windows powrshell) => Ethernet adapter vEthernet (WSL): => IPv4 Address

OR use yaml to up --buid

docker-compose.yaml:

version: '3.7'
services:
  postgres:
    image: postgres:9.6
    environment:
      - POSTGRES_USER=<user>
      - POSTGRES_PASSWORD=<password>
      - POSTGRES_DB=<db_name>
    ports:
      - "<host port>:<docker mapped port>"
  pgadmin:
    image: dpage/pgadmin4
    environment:
      - PGADMIN_DEFAULT_EMAIL=<email>
      - PGADMIN_DEFAULT_PASSWORD=<password>
    ports:
      - "<host port>:<docker mapped port>"

command run yaml:

docker-compose up --build

Why can't I connect to the postgres in Docker by the main.go in WSL2?

In WSL2, the IP address localhost points to the Linux environment itself, not to the Windows host. To access services running on the Windows host from WSL2, you need to use the IP address of the Windows host, which can be found by running the following command in PowerShell on the Windows host:

ipconfig

Look for the IP address under the Ethernet adapter vEthernet (WSL) section, which should be in the same network range as the {docker container default IP} subnet used by Docker for its containers. You can then use this IP address to connect to services running on the Windows host from within WSL2.

Some commands may be needed when first time get into postgres:

\c {tableName}
 >> get into table

\d 
 >> relations

GRANT ALL PRIVILEGES ON DATABASE {dataBase} TO {userName};

poolpay=# CREATE GROUP {group};
CREATE ROLE
poolpay=# GRANT SELECT, INSERT, UPDATE, DELETE ON {tableName} TO {group};
GRANT
poolpay=# GRANT {group} TO {userName};
GRANT ROLE
poolpay=# GRANT USAGE, SELECT ON SEQUENCE users_id_seq TO {group};

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO {group};
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO {group};
#docker #SQL #GO






你可能感興趣的文章

0 - 0.5 的實體伺服器架設

0 - 0.5 的實體伺服器架設

JAVA - 物件導向入門

JAVA - 物件導向入門

Web開發學習筆記16 — OOP(Object Oriented Programming)、Constructor Function、Class

Web開發學習筆記16 — OOP(Object Oriented Programming)、Constructor Function、Class






留言討論