[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






你可能感興趣的文章

[重新理解 C++]  TMP(2): variadic template parameter

[重新理解 C++] TMP(2): variadic template parameter

不要動!把表單交出來(1) - formData

不要動!把表單交出來(1) - formData

[Py 百日馬 Day 4-1] random module 常見方法 - 產生隨機數與亂數取樣/排序

[Py 百日馬 Day 4-1] random module 常見方法 - 產生隨機數與亂數取樣/排序






留言討論