Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help: How do I connect to a local pg database that is not in docker #533

Open
lucktoo opened this issue Apr 6, 2024 · 8 comments
Open

Comments

@lucktoo
Copy link

lucktoo commented Apr 6, 2024

请问怎么连接 不是在docker里面的,本地的pg数据库
求助

@tea-artist
Copy link
Contributor

To facilitate community search and reading, please use English for any discussions.
You can choose a docker-compose example and change the Postgres env variables in the .env file to your external database information as follows:

# Postgres
POSTGRES_HOST=teable-db
POSTGRES_PORT=5432
POSTGRES_DB=example
POSTGRES_USER=example
POSTGRES_PASSWORD=example2password

Also, remove the teable-db service in the docker-compose.yaml:

@tea-artist tea-artist changed the title 求助:请问怎么连接 不是在docker里面的,本地的pg数据库 Help: How do I connect to a local pg database that is not in docker Apr 6, 2024
@lucktoo
Copy link
Author

lucktoo commented Apr 7, 2024

请问是 改那个 .env文件呢?求助
请问怎么连接 不是在docker里面的,本地的pg数据库

文件下有2个.env文件
1./Users/bluesky/Documents/teable-develop/dockers/examples/standalone/.env
2./Users/bluesky/Documents/teable-develop/dockers/.env
3./Users/bluesky/Documents/teable-develop/dockers/examples/standalone/docker-compose.yaml 这个文件?
如下我注释掉了

teable-db:
  #   image: postgres:15.4
  #   restart: always
  #   ports:
  #     - '42345:5432'
  #   volumes:
  #     - teable-db:/var/lib/postgresql/data:rw
  #     # you may use a bind-mounted host directory instead,
  #     # so that it is harder to accidentally remove the volume and lose all your data!
  #     # - ./docker/db/data:/var/lib/postgresql/data:rw
  #   environment:
  #     - TZ=${TIMEZONE}
  #     - POSTGRES_DB=${POSTGRES_DB}
  #     - POSTGRES_USER=${POSTGRES_USER}
  #     - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
  #   networks:
  #     - teable-standalone
  #   healthcheck:
  #     test: ['CMD-SHELL', "sh -c 'pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}'"]
  #     interval: 10s
  #     timeout: 3s
  #     retries: 3

终端报错:
docker-compose up -d
service "teable-db-migrate" depends on undefined service teable-db: invalid compose project

@tea-artist
Copy link
Contributor

  1. change the env under standalone/.env
  2. replace standalone/docker-compose.yaml with the following content
version: '3.9'
services:
  teable:
    image: ghcr.io/teableio/teable:latest
    restart: always
    ports:
      - '3000:3000'
    volumes:
      - teable-data:/app/.assets:rw
      # you may use a bind-mounted host directory instead,
      # so that it is harder to accidentally remove the volume and lose all your data!
      # - ./docker/teable/data:/app/.assets:rw
    env_file:
      - .env
    environment:
      - TZ=${TIMEZONE}
      - NEXT_ENV_IMAGES_ALL_REMOTE=true
    networks:
      - teable-standalone
    depends_on:
      teable-db-migrate:
        condition: service_completed_successfully
  teable-db-migrate:
    image: ghcr.io/teableio/teable-db-migrate:latest
    environment:
      - TZ=${TIMEZONE}
      - PRISMA_DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}
    networks:
      - teable-standalone

networks:
  teable-standalone:
    name: teable-standalone-network
    driver: bridge

volumes:
  teable-data: {}

@lucktoo
Copy link
Author

lucktoo commented Apr 7, 2024

按你这个改了以后,docker里面这个启动以后又自动结束了,结束以后又自动启动,死循环
不知道是那有问题

@lucktoo
Copy link
Author

lucktoo commented Apr 7, 2024

✔ Network teable-standalone-network Created 0.3s
✔ Volume "standalone_teable-data" Created 0.0s
✘ Container standalone-teable-db-1 Error 34.1s
✔ Container standalone-teable-db-migrate-1 Created 0.1s
✔ Container standalone-teable-1 Created

@tea-artist
Copy link
Contributor

tea-artist commented Apr 7, 2024

The solution generated by gpt4, might be help

Based on the output you’ve shared, the standalone-teable-db-1 container encountered an error upon startup, but it seems there’s no longer a corresponding service definition in the new Docker Compose file. To address this issue, you may want to follow these steps:
1. Ensure All Services Are Stopped:
• Use the docker-compose down command to ensure all services are stopped and associated networks and containers are removed.
2. Clean Up Dangling Volumes and Networks:
• If you changed service names or removed services, there might be some volumes or networks not automatically deleted. You can use the following commands to list and remove these resources:
• List all volumes: docker volume ls
• Remove a specific volume: docker volume rm
• List all networks: docker network ls
• Remove a specific network: docker network rm
3. Review the Docker Compose File:
• Make sure the new Docker Compose file no longer includes any definitions related to the teable db service, including configurations for services, networks, and volumes.
4. Restart Services:
• Use the docker-compose up command to restart the services. If your Docker Compose file no longer contains the teable db service, it should not attempt to start this service.
5. Check Logs for Error Details:
• If you encounter errors during startup, you can use the docker-compose logs command to check the logs of the containers, which can be very helpful for diagnosing problems. Specifically, you should review the logs for the standalone-teable-db-1 container to understand why it encountered an error.
Please note, if you did not use the -v option with docker-compose down to remove volumes, previously created volumes might still exist and be reused upon the next startup. If you want to completely clean up all data and start fresh, you should include the -v option when executing docker-compose down.

@lucktoo
Copy link
Author

lucktoo commented Apr 7, 2024

2024-04-08 06:57:49
2024-04-08 06:57:49 PrismaClientInitializationError: Can't reach database server at 127.0.0.1:5432
2024-04-08 06:57:49
2024-04-08 06:57:49 Please make sure your database server is running at 127.0.0.1:5432.
2024-04-08 06:57:49 at r (/app/node_modules/.pnpm/@prisma+client@5.11.0_prisma@5.11.0/node_modules/@prisma/client/runtime/library.js:112:2493)
2024-04-08 06:57:49 at async Proxy.onModuleInit (/app/packages/db-main-prisma/dist/prisma.service.js:110:9)
2024-04-08 06:57:49 at async Promise.all (index 0)
2024-04-08 06:57:49 at async callModuleInitHook (/app/node_modules/.pnpm/@nestjs+core@10.3.5_@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/hooks/on-module-init.hook.js:43:5)
2024-04-08 06:57:49 at async NestApplication.callInitHook (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/nest-application-context.js:223:13)
2024-04-08 06:57:49 at async NestApplication.init (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/nest-application.js:100:9)
2024-04-08 06:57:49 at async NestApplication.listen (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/nest-application.js:169:33)
2024-04-08 06:57:49 at async bootstrap (/app/apps/nestjs-backend/dist/index.js:85:5)
2024-04-08 06:57:49 at async /app/apps/nestjs-backend/dist/index.js:31741:15 {
2024-04-08 06:57:49 clientVersion: '5.11.0',
2024-04-08 06:57:49 errorCode: 'P1001'
2024-04-08 06:57:49 }
2024-04-08 06:57:49
2024-04-08 06:57:49 Node.js v20.9.0
2024-04-08 06:57:49 Error: /app/node_modules/.pnpm/@prisma+client@5.11.0_prisma@5.11.0/node_modules/@prisma/client/runtime/library.js:112
2024-04-08 06:57:49 You may have to run ${$e("prisma generate")} for your changes to take effect.,this.config.clientVersion);return r}}parseEngineResponse(r){if(!r)throw new j("Response from the Engine was empty",{clientVersion:this.config.clientVersion});try{return JSON.parse(r)}catch{throw new j("Unable to JSON.parse response from engine",{clientVersion:this.config.clientVersion})}}async loadEngine(){if(!this.engine){this.QueryEngineConstructor||(this.library=await this.libraryLoader.loadLibrary(this.config),this.QueryEngineConstructor=this.library.QueryEngine);try{let r=new WeakRef(this),{adapter:t}=this.config;t&&Se("Using driver adapter: %O",t),this.engine=new this.QueryEngineConstructor({datamodel:this.datamodel,env:process.env,logQueries:this.config.logQueries??!1,ignoreEnvVarErrors:!0,datasourceOverrides:this.datasourceOverrides??{},logLevel:this.logLevel,configDir:this.config.cwd,engineProtocol:"json"},n=>{r.deref()?.logger(n)},t),qa++}catch(r){let t=r,n=this.parseInitError(t.message);throw typeof n=="string"?t:new R(n.message,this.config.clientVersion,n.error_code)}}}logger(r){let t=this.parseEngineResponse(r);if(t){if("span"in t){this.config.tracingHelper.createEngineSpan(t);return}t.level=t?.level.toLowerCase()??"unknown",fm(t)?this.logEmitter.emit("query",{timestamp:new Date,query:t.query,params:t.params,duration:Number(t.duration_ms),target:t.module_path}):gm(t)?this.loggerRustPanic=new ue(Hi(this,${t.message}: ${t.reason} in ${t.file}:${t.line}:${t.column}),this.config.clientVersion):this.logEmitter.emit(t.level,{timestamp:new Date,message:t.message,target:t.module_path})}}parseInitError(r){try{return JSON.parse(r)}catch{}return r}parseRequestError(r){try{return JSON.parse(r)}catch{}return r}onBeforeExit(){throw new Error('"beforeExit" hook is not applicable to the library engine since Prisma 5.0.0, it is only relevant and implemented for the binary engine. Please add your event listener to the process object directly instead.')}async start(){if(await this.libraryInstantiationPromise,await this.libraryStoppingPromise,this.libraryStartingPromise)return Se(library already starting, this.libraryStarted: ${this.libraryStarted}),this.libraryStartingPromise;if(this.libraryStarted)return;let r=async()=>{Se("library starting");try{let t={traceparent:this.config.tracingHelper.getTraceParent()};await this.engine?.connect(JSON.stringify(t)),this.libraryStarted=!0,Se("library started")}catch(t){let n=this.parseInitError(t.message);throw typeof n=="string"?t:new R(n.message,this.config.clientVersion,n.error_code)}finally{this.libraryStartingPromise=void 0}};return this.libraryStartingPromise=this.config.tracingHelper.runInChildSpan("connect",r),this.libraryStartingPromise}async stop(){if(await this.libraryStartingPromise,await this.executingQueryPromise,this.libraryStoppingPromise)return Se("library is already stopping"),this.libraryStoppingPromise;if(!this.libraryStarted)return;let r=async()=>{await new Promise(n=>setTimeout(n,5)),Se("library stopping");let t={traceparent:this.config.tracingHelper.getTraceParent()};await this.engine?.disconnect(JSON.stringify(t)),this.libraryStarted=!1,this.libraryStoppingPromise=void 0,Se("library stopped")};return this.libraryStoppingPromise=this.config.tracingHelper.runInChildSpan("disconnect",r),this.libraryStoppingPromise}version(){return this.versionInfo=this.library?.version(),this.versionInfo?.version??"unknown"}debugPanic(r){return this.library?.debugPanic(r)}async request(r,{traceparent:t,interactiveTransaction:n}){Se(sending request, this.libraryStarted: ${this.libraryStarted});let i=JSON.stringify({traceparent:t}),o=JSON.stringify(r);try{await this.start(),this.executingQueryPromise=this.engine?.query(o,i,n?.id),this.lastQuery=o;let s=this.parseEngineResponse(await this.executingQueryPromise);if(s.errors)throw s.errors.length===1?this.buildQueryError(s.errors[0]):new j(JSON.stringify(s.errors),{clientVersion:this.config.clientVersion});if(this.loggerRustPanic)throw this.loggerRustPanic;return{data:s,elapsed:0}}catch(s){if(s instanceof R)throw s;if(s.code==="GenericFailure"&&s.message?.startsWith("PANIC:"))throw new ue(Hi(this,s.message),this.config.clientVersion);let a=this.parseRequestError(s.message);throw typeof a=="string"?s:new j(${a.message}
2024-04-08 06:57:49
2024-04-08 06:57:49
2024-04-08 06:57:49 PrismaClientInitializationError: Can't reach database server at 127.0.0.1:5432
2024-04-08 06:57:49
2024-04-08 06:57:49 Please make sure your database server is running at 127.0.0.1:5432.
2024-04-08 06:57:49 at r (/app/node_modules/.pnpm/@prisma+client@5.11.0_prisma@5.11.0/node_modules/@prisma/client/runtime/library.js:112:2493)
2024-04-08 06:57:49 at async Proxy.onModuleInit (/app/packages/db-main-prisma/dist/prisma.service.js:110:9)
2024-04-08 06:57:49 at async Promise.all (index 0)
2024-04-08 06:57:49 at async callModuleInitHook (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/hooks/on-module-init.hook.js:43:5)
2024-04-08 06:57:49 at async NestApplication.callInitHook (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/nest-application-context.js:223:13)
2024-04-08 06:57:49 at async NestApplication.init (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/nest-application.js:100:9)
2024-04-08 06:57:49 at async NestApplication.listen (/app/node_modules/.pnpm/@nestjs+core@10.3.5
@nestjs+common@10.3.5_@nestjs+platform-express@10.3.5_@nestjs+websockets@_kuk2terzn3qzk2yl6blbmi4kjq/node_modules/@nestjs/core/nest-application.js:169:33)
2024-04-08 06:57:49 at async bootstrap (/app/apps/nestjs-backend/dist/index.js:85:5)
2024-04-08 06:57:49 at async /app/apps/nestjs-backend/dist/index.js:31741:15 {
2024-04-08 06:57:49 clientVersion: '5.11.0',
2024-04-08 06:57:49 errorCode: 'P1001'
2024-04-08 06:57:49 }
2024-04-08 06:57:49
2024-04-08 06:57:49 Node.js v20.9.0
2024-04-08 06:57:49 at file:///app/scripts/start.mjs:5:8
2024-04-08 06:57:49 exit code: 1

@tea-artist
Copy link
Contributor

To connect a Docker container to a local database running on your host machine, replace 127.0.0.1 with host.docker.internal in your database connection string. This special DNS name allows your container to access services on the host machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants