如何从 Github 代码空间连接到 Postgres
How to connect to Postgres from Github Codespaces
我正在试验 GitHub 代码空间,尝试使用 Node 和 Postgres 启动应用程序。
我选择了以下选项:
产生了以下 devcontainer.json
:
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version: 10, 12, 14
{
"name": "Node.js & PostgreSQL",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"sqltools.connections": [{
"name": "Container database",
"driver": "PostgreSQL",
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"database": "postgres",
"username": "postgres",
"password": "postgres"
}]
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg"
]
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [3000, 5432],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "node"
}
和docker-compose.yml
:
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
args:
# [Choice] Node.js version: 14, 12, 10
VARIANT: 14
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000
volumes:
- ..:/workspace:cached
# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db
# Uncomment the next line to use a non-root user for all processes.
# user: node
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
db:
image: postgres:latest
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward MongoDB locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
volumes:
postgres-data:
我的package.json
如下:
{
"dependencies": {
"pg": "^8.4.2"
},
"scripts": {
"start": "node index.js"
}
}
我的index.js
是这样的:
const { Pool } = require("pg")
const db = new Pool()
db.query(`CREATE TABLE IF NOT EXISTS testing(id SERIAL PRIMARY KEY);`)
运行 yarn start
产生以下错误:
codespace ➜ ~/workspace/codespace-demo (main ✗) $ yarn start
yarn run v1.17.3
warning package.json: No license field
$ node index.js
(node:1037) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:1037) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1037) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Done in 0.11s.
这让我相信 Postgres 实例没有启动并且 运行 在 Codespace 中。
我尝试取消注释关于转发端口的这一行:
"forwardPorts": [3000, 5432]
但运气不好。
我可能缺少一些关于 Docker 作品如何适合在这里演奏的见解,但如果有人能指出正确的方向,我会很高兴!
我能用两点来解决这个问题。
里面的文件仅仅加上一个.devcontainer
是不够的。您需要提交更改、推送它们、删除代码空间,然后创建一个新代码空间以反映更改。
在 .devcontainer/docker-compose.yml
中,我已将行 network_mode: host
添加到 db
服务。根据这个线程,这个要求很快就会被删除,但现在是必需的:https://github.community/t/cant-connect-to-postgres/142655/2?u=andyweiss1982
我正在试验 GitHub 代码空间,尝试使用 Node 和 Postgres 启动应用程序。
我选择了以下选项:
产生了以下 devcontainer.json
:
// Update the VARIANT arg in docker-compose.yml to pick a Node.js version: 10, 12, 14
{
"name": "Node.js & PostgreSQL",
"dockerComposeFile": "docker-compose.yml",
"service": "app",
"workspaceFolder": "/workspace",
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"sqltools.connections": [{
"name": "Container database",
"driver": "PostgreSQL",
"previewLimit": 50,
"server": "localhost",
"port": 5432,
"database": "postgres",
"username": "postgres",
"password": "postgres"
}]
},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint",
"mtxr.sqltools",
"mtxr.sqltools-driver-pg"
]
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [3000, 5432],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "yarn install",
// Uncomment to connect as a non-root user. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "node"
}
和docker-compose.yml
:
version: '3'
services:
app:
build:
context: .
dockerfile: Dockerfile
args:
# [Choice] Node.js version: 14, 12, 10
VARIANT: 14
# On Linux, you may need to update USER_UID and USER_GID below if not your local UID is not 1000.
USER_UID: 1000
USER_GID: 1000
volumes:
- ..:/workspace:cached
# Overrides default command so things don't shut down after the process ends.
command: sleep infinity
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
network_mode: service:db
# Uncomment the next line to use a non-root user for all processes.
# user: node
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
db:
image: postgres:latest
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
# Add "forwardPorts": ["5432"] to **devcontainer.json** to forward MongoDB locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
volumes:
postgres-data:
我的package.json
如下:
{
"dependencies": {
"pg": "^8.4.2"
},
"scripts": {
"start": "node index.js"
}
}
我的index.js
是这样的:
const { Pool } = require("pg")
const db = new Pool()
db.query(`CREATE TABLE IF NOT EXISTS testing(id SERIAL PRIMARY KEY);`)
运行 yarn start
产生以下错误:
codespace ➜ ~/workspace/codespace-demo (main ✗) $ yarn start
yarn run v1.17.3
warning package.json: No license field
$ node index.js
(node:1037) UnhandledPromiseRejectionWarning: Error: connect ECONNREFUSED 127.0.0.1:5432
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
(node:1037) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:1037) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Done in 0.11s.
这让我相信 Postgres 实例没有启动并且 运行 在 Codespace 中。
我尝试取消注释关于转发端口的这一行:
"forwardPorts": [3000, 5432]
但运气不好。
我可能缺少一些关于 Docker 作品如何适合在这里演奏的见解,但如果有人能指出正确的方向,我会很高兴!
我能用两点来解决这个问题。
里面的文件仅仅加上一个
.devcontainer
是不够的。您需要提交更改、推送它们、删除代码空间,然后创建一个新代码空间以反映更改。在
.devcontainer/docker-compose.yml
中,我已将行network_mode: host
添加到db
服务。根据这个线程,这个要求很快就会被删除,但现在是必需的:https://github.community/t/cant-connect-to-postgres/142655/2?u=andyweiss1982