我如何访问在docker-compose中运行的Postgres数据库,为什么在向Postgres添加数据时我的id表现得如此奇怪?

时间:2018-12-11 21:05:39

标签: postgresql docker docker-compose

我有两个表。一个名为Cinema的表包含所有电影院,另一个名为Room的表包含该电影院的所有房间。在房间里,有ID引用电影院表中的主键。

但是,当我添加一个电影院时,其获取ID:1,当我添加另一电影院时,其获取ID:2,但是当我添加参照主键电影院ID:1的房间时,添加的房间获取ID:3 ??

为什么?房间是否应该在自己的表格中获得ID:1?还是我误解了Postgres的工作原理?

我的电影院实体:

@Entity
class Cinema(

    @get:Id @get:GeneratedValue
    var id: Long? = null,

    @get:NotBlank @get:Size(max = 128)
    var name: String,

    @get:NotBlank @get:Size(max = 128)
    var location: String? = null,

    @get:OneToMany(mappedBy = "cinema", cascade = [CascadeType.ALL], fetch = FetchType.EAGER)
    var rooms: MutableSet<Room> = mutableSetOf()
)

还有我的房间实体:

@Entity
class Room(

    @get:Id @get:GeneratedValue
    var id: Long? = null,

    @get:NotBlank @get:Size(max = 128)
    var name: String,

    @get:ElementCollection
    @get:NotNull
    var seats: MutableSet<String>,

    @get:ManyToOne(fetch = FetchType.EAGER)
    @get:JoinColumn(name = "cinema_id")
    var cinema: Cinema? = null
)

我还运行一个flyway文件来创建数据库:

create sequence hibernate_sequence start with 1 increment by 1;
create table user_entity_roles (user_entity_username varchar(255) not null, roles varchar(255));
create table users (username varchar(255) not null, enabled boolean not null, password varchar(255), primary key (username));
alter table user_entity_roles add constraint FKsn3bllbt5h2wue4tckbylorlj     
foreign key (user_entity_username) references users;

现在,我想尝试访问在docker-compose中运行的Postgres数据库,以查看该数据库的外观。我如何访问它?

这是我来自该数据库的docker-compose日志:

postgres-cinema_1_aa7b4d8ccfa6 | The files belonging to this database system will be owned by user "postgres".
postgres-cinema_1_aa7b4d8ccfa6 | This user must also own the server process.
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | The database cluster will be initialized with locale "en_US.utf8".
postgres-cinema_1_aa7b4d8ccfa6 | The default database encoding has accordingly been set to "UTF8".
postgres-cinema_1_aa7b4d8ccfa6 | The default text search configuration will be set to "english".
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | Data page checksums are disabled.
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
postgres-cinema_1_aa7b4d8ccfa6 | creating subdirectories ... ok
postgres-cinema_1_aa7b4d8ccfa6 | selecting default max_connections ... 100
postgres-cinema_1_aa7b4d8ccfa6 | selecting default shared_buffers ... 128MB
postgres-cinema_1_aa7b4d8ccfa6 | selecting dynamic shared memory implementation ... posix
postgres-cinema_1_aa7b4d8ccfa6 | creating configuration files ... ok
postgres-cinema_1_aa7b4d8ccfa6 | running bootstrap script ... ok
postgres-cinema_1_aa7b4d8ccfa6 | performing post-bootstrap initialization ... ok
postgres-cinema_1_aa7b4d8ccfa6 | syncing data to disk ... ok
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | Success. You can now start the database server using:
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 |     pg_ctl -D /var/lib/postgresql/data -l logfile start
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | WARNING: enabling "trust" authentication for local connections
postgres-cinema_1_aa7b4d8ccfa6 | You can change this by editing pg_hba.conf or using the option -A, or
postgres-cinema_1_aa7b4d8ccfa6 | --auth-local and --auth-host, the next time you run initdb.
postgres-cinema_1_aa7b4d8ccfa6 | ****************************************************
postgres-cinema_1_aa7b4d8ccfa6 | WARNING: No password has been set for the database.
postgres-cinema_1_aa7b4d8ccfa6 |          This will allow anyone with access to the
postgres-cinema_1_aa7b4d8ccfa6 |          Postgres port to access your database. In
postgres-cinema_1_aa7b4d8ccfa6 |          Docker's default configuration, this is
postgres-cinema_1_aa7b4d8ccfa6 |          effectively any other container on the same
postgres-cinema_1_aa7b4d8ccfa6 |          system.
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 |          Use "-e POSTGRES_PASSWORD=password" to set
postgres-cinema_1_aa7b4d8ccfa6 |          it in "docker run".
postgres-cinema_1_aa7b4d8ccfa6 | ****************************************************
postgres-cinema_1_aa7b4d8ccfa6 | waiting for server to start....2018-12-11 20:47:19.832 UTC [44] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:19.916 UTC [45] LOG:  database system was shut down at 2018-12-11 20:47:19 UTC
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:19.933 UTC [44] LOG:  database system is ready to accept connections
postgres-cinema_1_aa7b4d8ccfa6 |  done
postgres-cinema_1_aa7b4d8ccfa6 | server started
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | waiting for server to shut down....2018-12-11 20:47:19.963 UTC [44] LOG:  received fast shutdown request
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:19.974 UTC [44] LOG:  aborting any active transactions
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:19.982 UTC [44] LOG:  worker process: logical replication launcher (PID 51) exited with exit code 1
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:19.984 UTC [46] LOG:  shutting down
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:20.050 UTC [44] LOG:  database system is shut down
postgres-cinema_1_aa7b4d8ccfa6 |  done
postgres-cinema_1_aa7b4d8ccfa6 | server stopped
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | PostgreSQL init process complete; ready for start up.
postgres-cinema_1_aa7b4d8ccfa6 |
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:20.139 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:20.139 UTC [1] LOG:  listening on IPv6 address "::", port 5432
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:20.147 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:20.203 UTC [53] LOG:  database system was shut down at 2018-12-11 20:47:20 UTC
postgres-cinema_1_aa7b4d8ccfa6 | 2018-12-11 20:47:20.235 UTC [1] LOG:  database system is ready to accept connections

0 个答案:

没有答案